• Resolved NFWRo

    (@nfwro)


    Hi there
    I really need some help with the php for do_shortcode in custom fields.

    I have a custom field (provided by the Custom Field Creator plugin) that outputs a styled div for customers testimonials on a page template with the following code in:

    <?php
    $testimonial = get_post_meta($post->ID, 'testimonial', true);
    foreach($testimonial as $testimonial){
    echo '<div class="testimonial">';
    echo '<p>' . $testimonial['testimonial-text'] . '</p>';
    echo '<p class="testimonialname">' . $testimonial['testimonial-name'] . '</p>';
    echo '</div>';
    }
    ?>

    I’m sure I need to use the `echo do_shortcode(‘[iscorrect]’.$text_to_be_wrapped_in_shortcode.'[/iscorrect]’);
    from the codex as I have an [expand][/expand] shortcode that creates a collapsible div when there is a lot of testimonial text. But I only want to invoke the code if the shortcode is present in the div.

    I have found loads of posts on blogs and entries on the forum here about how to use shortcodes in custom fields and templates, but nothing that I could get to work. Can anyone help me as to how to correctly insert this into my code so that it is only used when the shortcode is present in the custom field value?

    The page concerned is at http://www.bridgetglasgow.com/wpress/testimonials.html.

    Any help would be very greatly appreciated.

    NB I have put a post onto the plugin forum, but had no reply and I feel this is probably quite a generic question anyway.

Viewing 6 replies - 1 through 6 (of 6 total)
  • But I only want to invoke the code if the shortcode is present in the div.

    Just run do_shortcode() on your content. If no shortcode is present nothing will happen. I very much doubt that it is worth the effort to check first. At best you will see an execution difference 5 or 6 decimal places back and I am not sure which way it would fall. The overhead of searching your string and running a conditional may add some execution time or it may just be a wash.

    Replacing this,

    echo '<p>' . $testimonial['testimonial-text'] . '</p>';

    , with this,

    echo do_shortcode('[expand]'.$testimonial['testimonial-text'].'[/expand]');

    , should work. What have you tried? And what didn’t work? Sounds like your shortcode needs some Javascript too. Is that loading correctly?

    Thread Starter NFWRo

    (@nfwro)

    Thank you so much for your reply.

    I’m sorry, I must have over-edited my post and removed the plugin name that the [expand] shortcode comes from. It’s from the jQuery Collapse-O-Matic plugin, so yes there is code behind the [expand] shortcodes.

    I tried your suggestion, but it placed every one of the custom field text values into a collapsed div.

    I’ve tried a few things after searching various code sample and forum posts. TBH I’ve forgotten the semantics of a lot of them. The nearest I got was:
    echo ‘<p>’ . $testimonial[‘testimonial-text’];
    echo do_shortcode(‘[expand]’.$testimonial[‘testimonial-text’].'[/expand]’);
    echo ‘</p>’;`

    but that again added a collapsible div to the end of each testimonial-text value. So I thought that I would probably need to test the value to see if the [expand] tags are present before running the shortcode?

    Thanks again for your help here.

    Then what are you trying to do? If you add the shortcode to the custom meta field via the backend form you needn’t add it when you run do_shortcode. (Hopefully the brackets don’t get encoded). If you want to add it conditionally based on the length of the content then that is a different question than you originally asked.

    Thread Starter NFWRo

    (@nfwro)

    I tried adding the shortcode into the custom field via the backend form, but it doesn’t appear to get parsed that way and just appears in the text out as is. So I went searching for a way to display shortcodes from within custom fields and found do_shortcode.

    What I was hoping to do was that the person entering a testimonial into the custom field will make a decision as to whether it is too long to be presented as one block of text. They then input all the text and wrap the [expand][/expand] shortcode around the piece of the text that can be put into a ‘read more’ collapsible div. I really didn’t word my original question very well 🙂

    Ok. What you need to do is count the length of the content then decide whether to use the shortcode.

    if (100 < strlen($testimonial['testimonial-text'])) {
       echo do_shortcode('[expand]'.$testimonial['testimonial-text'].'[/expand]');
    } else {
       echo '<p>'.$testimonial['testimonial-text']'</p>';
    }

    That gives you a very simple count of the number of characters– letters, numbers, spaces, punctuation, etc– in the content. It will also count characters found in HTML markup, if any. It may not be sophisticated enough but that is the idea.

    Thread Starter NFWRo

    (@nfwro)

    Thank you! The only problem with that for me is that it would chop the testimonial midword. However, by using a trigger character that is entered into the testimonial custom field, I can extend your idea to run the do_shortcode when that character is encountered thus:

    <?php
       $testimonial = get_post_meta($post->ID, 'testimonial', true);
       foreach($testimonial as $testimonial){
       echo '<div class="testimonial">';
       $testimonialText = $testimonial['testimonial-text'];
    
       $caretPos = strpos($testimonialText, "^");
       if( $caretPos == FALSE ) {
       echo '<p>' . $testimonialText . '</p>';
       }
       else {
       $introText = substr( $testimonialText, 0, $caretPos );
       $expandText = substr( $testimonialText, $caretPos + 1 );
    
       echo '<p>' . $introText . '</p>';
       echo do_shortcode('[expand title="Show rest of testimonial"     swaptitle="Hide rest of testimonial"]<p>'.$expandText.'</p>[/expand]');
       }
    
       echo '<p class="testimonialname">' . $testimonial['testimonial-name'] . '</p>';
       echo '</div>';
       }
    ?>

    I wish I could say that I’d come up with that extended code myself, but I had to enlist the help of my live in C# expert! 🙂

    Thank you so much for your help in pointing me in the right direction.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Shortcodes in custom fields but only when present’ is closed to new replies.