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 badges2 Answers
Reset to default 0Try 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
评论列表(0条)