• I want to replace shortcodes with their respective output to the database. I was told this code does that, but I am new to wordpress development, I don’t know where to paste this function, and how to call it.

    function so_modify_the_content( $content ) {
        $prependContent = do_shortcode( 'pre_content_shortcode' );
        $appendContent  = do_shortcode( 'post_content_shortcode' );
    
        $content        = $prependContent . $content . $appendContent;
    
        return $content; 
    }
    
    add_filter( 'the_content', 'so_modify_the_content' );

    At least point me to articles mentioning how to use functions.

Viewing 7 replies - 1 through 7 (of 7 total)
  • @brvnbld
    You can add shortcodes in Posts and Pages or direct in theme/function file.
    For shortcodes,
    Process you need to follow to create new shortcode is:

    1.register shortcode with its paramenters and callback function
    2.create callback function and add logic
    3.prepare shortcode syntax and call in functions or theme file.

    For more details check the articles which I found it would help you:
    1.Article 1
    2.Article 2

    Thanks
    Suman W.

    Moderator bcworkz

    (@bcworkz)

    All custom work in WP should either be contained in a plugin or child theme. A simple custom plugin is easy to create and will work for most custom code. However, if you need to alter theme templates it’s better to use a child theme. Almost all you need to know about WP development is accessed through the Developer Portal. In particular the plugins link.

    The do_shortcode() function needs to be passed a string with a correctly formatted shortcode which includes the square bracket delimiters.
    do_shortcode('[my_shortcode att="my-value"]');

    Your code is dynamically bracketing all content with shortcode output (assuming you correctly pass shortcode strings) when the content is output to the browser. It’s not saving shortcode expansion in the DB. Dynamically adding content on output is the normal way to handle shortcodes. Storing shortcode expansions in the DB has several drawbacks.

    If you need to do this for all content, it may make more sense to customize the page’s template to bracket content output by calling the shortcode handler function directly and echoing out the returned string. Doing so would cut out unnecessary parsing of content.

    Thread Starter brvnbld

    (@brvnbld)

    function get_replaced_sourcecode_sc( $post ) {
    if ( empty($post) ) global $post;
    if ( empty($post) || ! isset($post->post_content) ) return false;
    $content = $post->post_content;
    if (
    preg_match_all( '/'. get_shortcode_regex() .'/s', $post->post_content, $matches )
    && array_key_exists( 2, $matches ) && in_array( 'media', $matches[2] )
    ) {
    foreach ( $matches[2] as $i => $sc ) {
      if ( $sc == 'media' )
        $now = $matches[0][$i];
        $replace = do_shortcode($now);
        $content = str_replace( $now, $replace, $content );
     }
    } return $content;
    }

    I tried this code, am I missing something?.

    The shortcode is [media:Raavan(2010)]

    The text Raavan(2010) is the post name. It varies with post.

    btw, thank you guys, you are just so helpful.

    Moderator bcworkz

    (@bcworkz)

    The do_shortcode() call will not honor the passing of parameters like that, they need to be passed like HTML attributes: [media post="Raavan(2020)"]
    “post” in this example has to be an attribute that the shortcode handler has defined and is expecting.

    To avoid potential issues with escaping or slashing of passed post titles, I recommend you pass post slugs instead of titles, so perhaps [media post="raavan2020"]

    I don’t see the point in parsing content for [media] shortcodes and replacing them with their expanded content. Why not just let WP expand shortcodes as it normally would?

    Thread Starter brvnbld

    (@brvnbld)

    1.Actually my website is in google compute engine, and it has only 10gb of space. The space in compute engine is costly. So I have created a good storage bucket, and am storing files there.
    This plugin,(media downloader), lists files only from locally hosted folders.(with their meta data).
    If I convert the shortcode to static, then I can replace the urls alone to point to the bucket.
    2.Plugins causes heavy load on website which in turn makes website slower.
    3.Shortcode’s content cannot be searched.

    Thread Starter brvnbld

    (@brvnbld)

    `function updatesc_database( $post ) {
    if ( empty($post) ) global $post;
    if ( empty($post) || ! isset($post->post_content) ) return false;
    $content = $post->post_content;
    $shortc1='[media:’;
    $foldername=get_the_title( $post );
    $shortc2=$foldername.’]’;
    $shortwh=’\”.$shortc1.$shortc2.’\”;
    $replace = do_shortcode( $shortwh );
    $content = str_replace( $shortwh, $replace, $content );
    return $content;
    }’

    Will this work?

    Moderator bcworkz

    (@bcworkz)

    Nothing you do with “the_content” filter will generate static post content. It’s a dynamic filter the applies changes on output. The data saved remains unchanged. You can alter content data before it’s inserted into the DB with “wp_insert_post_data” filter. Your callback is passed an array of post data. Alter the “post_content” element of the passed array to alter post content just before it is saved.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to replace shortcode with its result into the database?’ is closed to new replies.