php - Fix common misspellingserrors based on array of replacements

I'm trying to set up a function to fix common content errors on a site. I want to take an array of post fields $che

I'm trying to set up a function to fix common content errors on a site. I want to take an array of post fields $check_fields and check them for common errors as set up in an array called $replacement_terms.

I have the following so far:

$check_fields = array(
    "post_title",
    "post_content"
);

$replacement_terms = array(
    "™™,™",
    "®®,®",
    "  , "
);

foreach ( $check_fields as $field ) {

    foreach ( $replacement_terms as $term ) {

        $replacement = explode( ",", $field )

        $fixed_content = str_replace( $replacement["0"], $replacement["1"], $field);

        update_post_meta( $post_id, $field, $fixed_content );

    }

}

Any tips how I can get it working properly/optimize it a bit?

I'm trying to set up a function to fix common content errors on a site. I want to take an array of post fields $check_fields and check them for common errors as set up in an array called $replacement_terms.

I have the following so far:

$check_fields = array(
    "post_title",
    "post_content"
);

$replacement_terms = array(
    "™™,™",
    "®®,®",
    "  , "
);

foreach ( $check_fields as $field ) {

    foreach ( $replacement_terms as $term ) {

        $replacement = explode( ",", $field )

        $fixed_content = str_replace( $replacement["0"], $replacement["1"], $field);

        update_post_meta( $post_id, $field, $fixed_content );

    }

}

Any tips how I can get it working properly/optimize it a bit?

Share Improve this question asked Aug 26, 2019 at 22:14 warm__tapewarm__tape 611 silver badge11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Try understanding the example on the link given below, most probably it will help: https://www.sitepoint/find-correct-misspelled-words-pspell/

One optimization you could make is to use arrays as str_replace "find" and "replace" params you will avoid having a loop inside a loop.

Other than that it seems you are trying to save post_title and post_content in the post meta, but this data is not stored in post meta but rather in the post itself.

Your function for fixing the spelling error could look like this

function fix_spelling_errors( $post_id ) {
    global $wpdb;

    $check_fields = array(
        "post_title",
        "post_content"
    );

    $replacement_terms = array(
        "™™,™",
        "®®,®",
        "  , "
    );

    $terms_find = array();
    $terms_repl = array();

    foreach( $replacement_terms as $terms ) {
        $term = explode( ",", $terms );
        $terms_find[] = trim( $term[0] );
        $terms_repl[] = trim( $term[1] );
    }

    $post = get_post( $post_id );

    foreach( $check_fields as $field ) {

        if( isset( $post->$field ) ) {
            $fixed = str_replace( $terms_find, $terms_repl, $post->$field );
            $sql = $wpdb->prepare( "UPDATE {$wpdb->prefix}posts SET `{$field}` = %s WHERE ID = %d", $fixed, $post_id );
            $wpdb->query( $sql );

        } else {
            $to_fix = get_post_meta( $post_id, $field, true );
            $fixed = str_replace( $terms_find, $term_repl, $to_fix );
            update_post_meta( $post_id, $field, $fixed );
        }
    }

} );

Then you could use it as

fix_spelling_errors( 100 );

where 100 is an ID of a post in which you want to fix the spelling mistakes.

You did not mention where you would like to use this function if you would like to execute it when a post is saved from wp-admin panel then you can extcute it with a save_post action like this

add_action( "save_post", "fix_spelling_errors" );

although note that while the spelling errors will be fixed in the database the non-fixed texts will show in wp-admin / Posts while editing the post if you are using Gutenberg (you will need to refresh the page to show fixed texts).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208756a4616718.html

相关推荐

  • php - Fix common misspellingserrors based on array of replacements

    I'm trying to set up a function to fix common content errors on a site. I want to take an array of post fields $che

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信