• Hi,
    I wonder if there is a way to automatically insert a shortcode to post content when pressing publish button. For example, the post is

    This is a test post!

    After I press publish, the post content (in the database) will be

    This is a [shortcode] test [/shortcode] post!
    This rule applies to every post that I publish.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    I’m not aware of a plugin to do that but it may be easier to just filter the content and apply the shortcode that way. Are you trying to implement a shortcode on specific word or words?

    Filtering the_content with your own small plugin may be the way to go.

    https://codex.www.ads-software.com/Plugin_API/Filter_Reference/the_content

    Moderator bcworkz

    (@bcworkz)

    Alternately, if you really want the shortcode to be part of the post stored in the DB, you could hook ‘wp_insert_post_data’ and insert the shortcode into the proper spot. You would have to verify this hasn’t already been done, as this filter fires on updates and other actions besides post publish. Thus maybe not as easily done.

    Often more than one way to do something!

    Thread Starter cubryto

    (@cubryto)

    Thank you. I didn’t know I can filter the content. I solved by filter the content like Jan request. Now I’m stuck the word count for post. Currently I have a custom field called ‘wordcount’ to store the word counter using this code:
    function add_custom_field_automatically($post_ID) {
    global $wpdb;
    global $post;
    $content = $post->post_content;
    $wordcount = sizeof(explode(” “, $content)); //Ya this is an estimation word counter
    update_post_meta($post_ID, ‘wordcount’,$wordcount, true);
    }
    The problem is that $post->post_content has no content when pressing ‘publish’ button. Is there anyway that I can get the pre saved content?

    To people who is looking for an answer for my previous situation , I use this function:
    function function-to-modify-content($content){
    //filter the content
    return $content;
    }
    add_filter(‘content_save_pre’,’function-to-modify-content’);

    Moderator bcworkz

    (@bcworkz)

    That would be the ‘wp_insert_post_data’ filter. The good part is the same filter will redo the word count if the post is edited.

    automatically insert specific text into post.
    to do this, insert one of the following codes into functions.php:

    1)
    function add_before_content($content) {
    if ( ‘page’ == $post->post_type ) return $content .’Default page content.’;
    if ( ‘post’ == $post->post_type ) return $content .’Default post content.’;
    }
    add_filter(‘the_content’, add_before_content);

    2)

    function add_before_content($content) {
    return ‘Default Message’.$content;
    }
    add_action(‘publish_post’,add_before_content);
    add_action(‘update_post’,add_before_content);
    add_filter(‘the_content’, add_before_content);

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Auto insert shortcode to content.’ is closed to new replies.