0

I want to get the wp_editor (tinymce) content then find a sentence by specific word with regex. However, have no idea how to get the content

When a post is published or updated, the function will get the content and then find a sentence by the specific word.

My regex to get a whole sentence:

$regex = '/<p>Chapter(.*?)<\/p>/'; //Specific Word = Chapter
$chapnumber = '';
$chaptitle = '';
$chapcontent = "<p>Chapter 136 – Tibet and West Turk</p>"; //This should be the content
if (preg_match($regex, $chapcontent, $matches)) {
    echo $matches[1];
}

Any solution about this?

0

I can't comment yet... so I will use an answer, I think you need to do it with 'wp_insert_post_data' filter.

You can see this in line 3523 here: https://developer.wordpress.org/reference/functions/wp_insert_post/

Your function need to have a parameter, which is an array. From there you should get the content from 'post_content' key.

add_filter( 'wp_insert_post_data', 'example' );
function example( $data ){
    $post_content = $data['post_content'];
    // ... do your stuff
    $data['post_content'] = $post_content;
    return $data
}

Note: this will not get the tinymce content, This will get the data right before it's introduced in database. You should also take some attention at shortcodes, I don't think that you can modify the content in shortcodes via that filter. Hope it helps you, or someone else.

  • It seems still can't get the content, I try it with update_post_meta as a test and resulting nothing to set/save content to value of a custom field. (The content test has one sentence only) – niznet Feb 17 at 10:43
0

You could hook a filtering function to save_post action and then get the post content either from the $_POST variable or by using get_post($post_id) for filtering.

0

Fixed by myself, instead of doing an ajax. This more simpler to me:

$post_content = wpautop( $post->post_content );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.