php - Is there a way to get wp_editor (tinymce) content?

admin2025-01-08  4

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?

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?

Share Improve this question asked Feb 17, 2019 at 9:37 niznetniznet 231 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

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

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

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.

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267073a1171.html

最新回复(0)