A
A
Alexey selftrips.ru2018-03-14 14:46:22
WordPress
Alexey selftrips.ru, 2018-03-14 14:46:22

Is it possible to "filter" content instead of a shortcode?

Is it possible to do a content filter instead of registering a shortcode? What is bad/good about this solution?
Those.

function my_funct($content){
    global $post;
  $out = $content;
здесь ищем  в $out [my_shortcode] и если находим, заменяем на нужный код
  return $out;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan Hasanli, 2018-03-14
@azerphoenix

Of course, you can filter the content.
Here is an example of content filtering that I use. The following function filters the content (the_content) and adds nofollow noopener noreferrer to all outgoing links

function add_nofollow_content($content) {
  $content = preg_replace_callback(
    '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
  function($m) {
    if (strpos($m[1], "md7.info") === false)
    return '<a href="'.$m[1].'" rel="nofollow noopener noreferrer" target="_blank">'.$m[2].'</a>';
    else
    return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
  },
  $content);
    return $content;
  }
add_filter('the_content', 'add_nofollow_content');

I
Igor Vorotnev, 2018-03-15
@HeadOnFire

First, you can filter content with regular expressions. And it's slow.
Second, shortcodes also work on regular expressions, all of a sudden.
Third - if you are going to filter exactly [my_shortcode], then what is the profit? Or am I missing something?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question