0

I have simple post with a shortcode:

[xx]http://harrix.org/1.txt[xx]

And an external file which has the content containing a WordPress shortcode:

[strong] g[/strong]

I try add my own tag to load the external file:

<?
add_filter('the_content', 'WorkWithTags');
function WorkWithTags($content){
$Tag ="xx";

if (preg_match_all('~\['.$Tag.'\](.*?)\[/'.$Tag.'\]~is', $content, $arr)) 
{ 
   foreach ($arr[1] as $value) 
   {
      $newvalue = @file_get_contents($value);//Main String

      $content=str_replace($value, $newvalue, $content);
   }

   //Delete tags
   $tr=array('['.$Tag.']'=>'','[/'.$Tag.']'=>'',); 
   $content=strtr($content,$tr);
}
return $content;
}
?>

Adding file works good. But the external file contains shortcodes of WordPress. How can I get WordPress to execute all of the shortcodes from this external file?

Update.

I try use code by s_ha_dum in functions.php:

function WorkWithTags($atts,$content){
  $atts = shortcode_atts(
    array(
      'tag' => 'xx'
    ),
    $atts
  );
  $fo = $atts['tag'];
  $content = file_get_contents($fo);//Main String
  return do_shortcode($content);
}
add_shortcode('wwt','WorkWithTags');

enter image description here

Then I add this shortcode in post:

Text. Text. Text. Text. Text. Text. Text. Text. 

[wwt tag="http://harrix.org/1.txt" /]

Text. Text. Text. Text. Text. Text. Text. Text.

enter image description here

And I see that in preview of post:

enter image description here

I can not run shortcodes in an external file.

0

Shortcodes and Tags

At first, please keep in mind that your understanding of tags is actually a shortcode.

A Tag is used to add meta information to content, while a shortcode is used for adding functionality, usually to be called from inside the content.

To your Problem

As @s_ha_dum pointed out, you should implement your WorkWithTags-function as a Shortcode, so you do not have to add an extra filter to your content, searching for your shortcode.

To get it to work like you implemented it, you have @s_ha_dum's method, or you could alter your shortcode and function like this:

[xx]http://harrix.org/1.txt[/xx] //note that the second xx closes with the / before the shortcode-identifier.

Next, you add this function as a shortcode.

Keep in mind, there is already a filter on the_content looking for all the shortcodes, so you do not have to implement this yourself.

function WorkWithTags( $atts, $content ){
    $yourfile = esc_url( $content ); // clean the input
    $contents = file_get_contents( $yourfile ); // get the file
    return do_shortcode( $contents ); // search the content of your file for all registered shortcodes
}
add_shortcode('xx','WorkWithTags');

This should set you up nicely, although the preferable method is to implement your shortcode like [xx url="yoururl" /]. In this case, the url will not be in your $content of the function WorkWithTags, but in the $atts.

All registered Shortcodes will be executed on the contents of $yourfile. If they are not, make sure they are registered.

One last thing, if you use an external file to be included and "executed", make sure that you do not call an infinite look by placing another [xx] shortcode in your external file.

  • Yes, I mixed tags with shortcodes. I mean shortcodes. Your code, code by s_ha_dum and my code works. And these codes are added text from an external file. And your code easier then my code. But the problem is that the shortcodes in the external file are not run. How can you call them? Is there a function that would be processed to be added from an external text file as a post? – user1680906 Feb 27 '14 at 14:34
  • Are you sure your shortcode [strong] is registered? – fischi Feb 27 '14 at 16:31
0

What you are doing looks overly complicated to me. The Core shortcode system should be able to handle this. I think you are looking for something like this:

function WorkWithTags($atts,$content){
  $atts = shortcode_atts(
    array(
      'tag' => 'xx'
    ),
    $atts
  );
  $fo = get_template_directory().'/'.$atts['tag'];
  $content = file_get_contents($fo);//Main String
  return do_shortcode($content);
}
add_shortcode('wwt','WorkWithTags');

// test it
echo do_shortcode('content with a shortcode: [wwt tag="codesnippet.php" /]');

The [wwt tag="xx" /] shortcode will include file xx and run do_shortcode on the contents. I think that is what you mean by "get the Wordpress to perform all of the tags from this external file".

Note that that code loads a file from the wp-content/your-theme directory of the WordPress install. If you are trying to load a resource via URL this line:

$fo = get_template_directory().'/'.$atts['tag'];

... needs to be ...

$fo = $atts['tag'];

... though I admit that that doesn't make much sense to me since you are attempting to parse shortcodes in that file.

  • Thank you! Did not know about this method. But the main problem is not solved. Contents of the file is displayed, but the tags from this external file will not run. In an example file the text does not bold. – user1680906 Feb 27 '14 at 4:47
  • What "tags" need to run? Your question is not clear. – s_ha_dum Feb 27 '14 at 13:33
  • The external file contains shortcodes as usual post. For example, it contains a shortcode [strong] for bold text. But shortcodes are not run with the addition of the external file. In example I see that: [strong] g[/strong] But I want see that: g – user1680906 Feb 27 '14 at 14:42
  • My code will process shortcodes in the included file. I tested that. That is what the call to do_shortcode() is for. – s_ha_dum Feb 27 '14 at 14:46
  • I added information about using your code in question. I can not run shortcodes in an external file. Maybe my problem is because I add shortcode in the post, but you write somewhere through do_shortcode(). – user1680906 Feb 27 '14 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.