Support » Fixing WordPress » Content and Title filters, shortcodes

  • 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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Content and Title filters, shortcodes’ is closed to new replies.