WordPress.org

Ready to get started?Download WordPress

Forums

Use of do_shortcode instead of the_content (7 posts)

  1. bdsr
    Member
    Posted 2 years ago #

    I finally found a way to change the content of a widget from within a wordpress page - this was important because I want the owners of the site to be able to edit it without getting into trouble.

    The way I have done it is to use "echo apply_filters('the_content', $p->post_content);" in my code. To my distress though, having got this to work, I then fell across this page http://kovshenin.com/2011/12/the-content-filter-3720/ which warns against applying the_content filters. It suggests instead to use wpautop and do_shortcode.

    I can't work out how to use those alternatives. Can anyone advise please:
    1. is the advice against the use of the_content filters correct; and
    2. if so how, instead, do I use the alternative suggested. I have looked in the wordpress codex for advice on do_shortcode but the instructions are not detailed enough for me to understand.

    Thanks for any help.

    B

  2. vtxyzzy
    Member
    Posted 2 years ago #

    I believe the author was warning against using apply_filters in situations where a plugin might add code to the content and you would not want that code in the location you are applying the filters.

    If you want to use wpautop and do_shortcode instead, you would create a shortcode to enclose the parts of the text that you want to show in the widget and format what it returns with wpautop. Then the owners of the site would just need to remember to enclose the text with the shortcode.

    I have not tested this, but if your shortcode is '[widget-text]` a filter function like this should work:

    function widget_text_func($atts,$content='') {
       return wpautop($content);
    }
    add_filter('widget_text','widget_text_func');
  3. bdsr
    Member
    Posted 2 years ago #

    Thanks vtxyzzy for getting back to me.

    I have never used shortcodes before, so please bear with me.

    Because I wasn't sure about creating a shortcode or where to put the function I took an example from the wordpress codex and copied the following into my functions.php file as a trial:

    function foobar_func( $atts ){
    return "foo and bar";
    }
    add_shortcode( 'foobar', 'foobar_func' );

    function bartag_func( $atts ) {
    extract( shortcode_atts( array(
    'foo' => 'something',
    'bar' => 'something else',
    ), $atts ) );

    return "foo = {$foo}";
    }
    add_shortcode( 'bartag', 'bartag_func' );

    I expected that when I inserted [foobar] and [bartag] into my code that I would get back something other than [foobar] and [bartag] - but that is all that showed on the page!

    Is the functions.php file the place to put the code and, if so, what else should I have inserted for this to return something other than the name of the shortcode please?

    Thank you so much

  4. vtxyzzy
    Member
    Posted 2 years ago #

    The functions.php file is the correct place to put the code for the functions.

    Where did you put the [foobar] tag? Shortcodes should normally go in the text of a Page or Post.

  5. bdsr
    Member
    Posted 2 years ago #

    I just put it in the source code for this page:

    http://www.capacar.org.au/word/?page_id=180

    As you can see it isn't doing what one would expect. I just put in [foobar]. Is that correct?

    Thanks again.

  6. Pioneer Valley Web Design
    Member
    Posted 2 years ago #

    in HTML view?

  7. bdsr
    Member
    Posted 2 years ago #

    I originally put it in using the Source tab in CKEditor. I have just gone in and re-entered it using the HTML view, just in case, but it has made no difference.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.