What is short-code?
In WordPress some plugins comes with features like short-code. It work like this... You just install plugin and lets say its task is to show a contact form wherever you want in your page so just put
[[form]] on that place from back-end CMS and it reflects a contact form on corresponding page on front end site.
Another example is let's say link that say
Share on Twitter to the user so just put
[[twitter]] wherever you want to show your user
Share post on Twitter
I Pick both the examples from
THIS LINK so for more details please read it. and to understand better see the snapshots shown.
What I want to achieve?
I want similar feature in a custom CMS that I am developing. I try to Google a lot but everywhere either i found WordPress implementation or nothing except on
THIS LINK[
^] in reply of Binyamin where its clearly shown how to implement this.
What's my Idea to Implement this?
So the Idea is to put my code in separate file and import the content of file like include("file") in PHP to the place wherever I put short-code. In my format that I have implemented short-code looks like this
{{keyword}} and the keyword is the name of file that i wants to include.
I achieve this as
suggested by Binyamin[
^] as shown below in my code.
<?php
$query = "<DATABASE QUERY>";
$result = mysqli_query($con,$query) or die(mysqli_connect_error());
if($row = $reqult->fetch_array(MYSQLI_BOTH)){
$str = $row["page_content"];
echo preg_replace_callback("/\{{(.*?)\}}/", function($m){
return include($m[1]);
}, $str)
}
?>
This short-code {{keyword}} is saved in database along with other content of the page as it is and while fetching the content to front end from database it search for short-code and replace it with content of file with name of keyword that is used as keyword in short-code
STEPS TOWARDS ACHIEVING
1. Above code rejects every other content and display only content of file that is included using short-code. Along with this it is also printing one line for each short-code at bottom after including all the file. This additional line contain nothing but
numeric one "1".
To solve this issue i simply print the content fetched from database before calling short-code implementation but then it show my short-code itself on front end so to remove this i put another filter as follow
echo preg_replace("/\{{(.*?)\}}/","",$row["page_content"])
Magically this also hide that
numeric one "1" additional line.
2. Now the only issue remaining is that because of such implementation as i shown above I am unable to include content of file [of shortcode] in between of the content.
PICTORIAL ILLUSTRATION
I have made a pictorial illustration of problem. In which i have shown both the files
videos and
testimonials. shown the CMS input from admin panel, its source code and what it saved to MySQL database.
In the illustration please see carefully the expected output and output what i am getting right now.
See the illustration here[
^]
For large images and for better understanding you can see
this dropbox folder[
^]
WHAT I AM SEEKING
As i myself have implemented it in a way that everything stored in database will print first before implementation of short-code and so i am getting this output. I think i need to remove that part and need to modify
preg_replace_callback() function itself to return not only short-code implementation but also other content. Can anyone tell me what modification i should do to achieve my goal?
Thanks in advance
!!!!!!!!! UPDATE !!!!!!!!
This is an old issue still unsolved. Anyone out there who can resolve this issue asap as its urgent now.