Insert specific content.

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 30, 2013   (RSS)

By Toledoh - August 29, 2013

Hi Guys,

I know this has been asked (and answered) before, but I can't find the answer.

I have a number of files that I want to include from within a WYSIWYG field...  ie.  I want to be able to have something like ###insertOne### and have this display as <?php include("insertOne.php"); ?>

How do I achieve that?

Cheers,

Tim (toledoh.com.au)

Hi Tim,

Does your insertOne.php file contain dynamic PHP content, or is it just html code? If its just html code the easiest way to swap out the placeholder is like this:

  $filterOn = '#insert_content#';

  //Get the new content to display from a file
  $new_content = file_get_contents('test.php'); 

  //Add new content to content variable
  $blogRecord['content'] = str_replace($filterOn, $new_content, $blogRecord['content']);

So the placeholder in this example is called #insert_content#, the content of test.php is stored as a string in the variable new_content. Then str_replace is used to swap the placeholder with the string of content from test.php. 

If you have PHP code in insertOne that needs to be run on the page, I would recommend using this method instead:

  $filterOn = '#insert_content#';

  //Check if the string contains the filterOn placeholder, if it does store the num where it starts in a variable
  if($startNum = strrpos( $blogRecord['content'] , $filterOn)){
    
    //Display the first half of our content string before the placeholder
    echo substr($blogRecord['content'],0,$startNum);
    
    //Include our content from test.php
    include('test.php');

    //Display the second half of the content after the placeholder. Add the length of the placeholder to ensure the placeholder isn't displayed. 
    echo substr($blogRecord['content'],$startNum+(strlen($filterOn)));
  }else{
    
    //If the string doesn't contain our placeholder, display it like normal.
    echo $blogRecord['content'];
  }

So the above system first checks if the blog content contains the filter on variable, and number of digits into the string where it starts. It then displays the first half on the string before the placeholder. Then the content of test.php is included and then finally the second half of the content is displayed.

All of the above is just example code, so you might have to make a few changes to get it working with your pages.

Let me know if you have any questions. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - August 29, 2013

That works great Greg - and yes, it's dynamic content.

However, I've got multiple placeholders in a single section... ie.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

#content-1#

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

#content-2#

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Can I do that?

Cheers,

Tim (toledoh.com.au)

By Toledoh - August 30, 2013

Wow - that's really cool.  It gives a whole new level of flexibility!

Cheers,

Tim (toledoh.com.au)