WordPress.org

Ready to get started?Download WordPress

Forums

Content and Title filters, shortcodes (3 posts)

  1. miniCarl
    Member
    Posted 2 years ago #

    I have this shortcode:

    // Highlighting shortcode
    function caption_shortcode($atts, $content = null){
    	return '<span class="highlight">'.$content.'</span>';
    }
    add_shortcode('h', 'caption_shortcode');

    And printing my content using:

    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;

    This is working great =)

    However, this code is kinda copy pasted, and im curious about what exactly its doing. From experimenting I learned that without the apply_filters() function, my shortcodes did not work. But the str_replace() function didn't seem to make any difference on my theme.

    Also, I wanted to use shortcodes in my title, so I did the same thing to it as with the content:

    $title = $post->post_title;
    $title = apply_filters('the_content', $title);
    $title = str_replace(']]>', ']]>', $title);
    echo $title;

    Is it wrong of me to use a the_content filter on the title?
    And still, is the str_replace() necessary?
    Is there a better way to do this?

  2. miniCarl
    Member
    Posted 2 years ago #

    Ok, I found a slightly better way to do things...
    Instead of using the the_content filter on the title, i now use
    $title = apply_filters('the_title', $title);
    ...And this works with the shortcode when I add this to my functions.php file:
    add_filter('the_title', 'do_shortcode');

    ...Still looking for improvements

  3. miniCarl
    Member
    Posted 2 years ago #

    nothing?

Topic Closed

This topic has been closed to new replies.

About this Topic