Shortcode for Wordpress

Asked

Viewed 112 times

1

I am using the IP API for geolocation in a wordpress plugin, the plugin is ready and works normally! I want to add the following:

In one of the fields of the plugin, the user can insert a text and at the end of this text, except that this field does not accept shortcode, it is a type of text field, without editor or anything, I will attach the image. The shortcode is already ready, just need the field accept the shortcode, for now it only brings the shortcode as text.

Exit Code

$loop_link_rows .= '<div class="coluna_relacionados_fake"><a rel="nofollow" target="_blank" href="/?action=count&id=' . $row->meta_id . '"><img src="'.content_url().'/uploads'.$row->meta_attachment.'" alt="'.esc_attr( stripcslashes($row->meta_title)).'"></a><a rel="nofollow" target="_blank" href="/?action=count&id=' . $row->meta_id . '">'.$row->meta_content.'</a></div>';

Shortcode no campo de texto

1 answer

0


For the shortcode to work you need to call a filter, or activate it directly.

The simplest way is to call the filter the_content, that applies all the filters defined for the default content, including activating the shortcodes. Serves better if you have text along with the shortcode.

// Supondo que sua meta-key seja "chave"
$meta_value = get_post_meta( $post->ID, 'chave', true );
echo apply_filters( 'the_content', $meta_value );

The other way is to call do_shortcode() directly.

// Supondo que sua meta-key seja "chave"
$meta_value = get_post_meta( $post->ID, 'chave', true );
echo do_shortcode( $meta_value );

Applying in the code:

$loop_link_rows .= '<div class="coluna_relacionados_fake">
    <a rel="nofollow" target="_blank" href="/?action=count&id=' . $row->meta_id . '">
        <img src="'.content_url().'/uploads'.$row->meta_attachment.'" alt="'.esc_attr( stripcslashes($row->meta_title)).'">
    </a>
    <a rel="nofollow" target="_blank" href="/?action=count&id=' . $row->meta_id . '">'
        .apply_filters( 'the_content', $row->meta_content ).
    '</a>
</div>';
  • I tried to do here, but it didn’t work, I just applied the filters to my function.. At least I understood that was it! I will post a Photo of what I did.

  • You should apply the filters where you need the shortcode to be expanded. Usually this is in the template where it will be used, when printing on the same screen. Remembering that shortcodes do not expand within input or textarea

  • I need the $Row->meta_content field to accept shortcodes, so I did that. http://imgur.com/a/0Qu7f Mas, it is a textarea field, if that is the problem, which field indicates me?

  • So, apply_filters() returns the content with the expanded shortcode but you are not using this return. You need to put this where is the second mention to $row->meta_content there. Copy this excerpt there in the question and I update with the example.

  • I updated the question

  • Updated with the code.

  • I think we’re almost there, there’s only one bug on the front now http://imgur.com/a/bxlq4

  • This error is not related, it is the Xdebug that is complaining about the number of nesting levels. The solution is to disable the Xdebug or increase the limit, see here http://stackoverflow.com/questions/8656089/solution-for-fatal-error-maximum-function-nesting-level-of-100-reached-abor

  • Show, I’ll look for how to resolve this error!

  • 1

    With do_shortcode($Row->meta_content) do not give this error any! Vlw by help!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.