Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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"]; // column of database which contain content of page

    // replace all occurance of {{<KEYWORD>}} with content of similar file name
    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
PHP
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.
Posted
Updated 28-Sep-16 19:20pm
v14
Comments
George Jonsson 14-Nov-15 21:25pm    
Don't use capitals to emphasize your question. It is like shouting and kind of rude.
Hitesh Rohilla 15-Nov-15 11:12am    
I apologize if it sounds rude but i never meant that. My English is not good may be that's the issue... Nobody teach me ever that upper case meaning is shouting or being rude. Similar kind of response i get around 7-8 years back from a group moderator in Orkut community. At that time people started a long discussion thread on the topic that upper case mean shouting or not but that never ends to a conclusion.

Well I am sorry if it felt like that to you...
George Jonsson 15-Nov-15 11:20am    
It is good to learn something new everyday. :-)
George Jonsson 14-Nov-15 21:37pm    
It is a very wordy question, but I have to admit I cannot figure out what it is you want to do.
What is short-code in this context? A string, a phone number or the old computer language?
Hitesh Rohilla 15-Nov-15 11:17am    
It was hard to find solution so i thought to put everything in one post so that if someone else is searching for same he/she could find it here. Sites like stackoverflow and codeproject are best place for this.

I will modify question so that it will easy to understand problem statement

And you will understand short code if you would be a developer or user of WordPress. In WordPress some plugins like contact form plugins comes with short-codes. you just design your form layout of contact form in plugin area and assign a short-code or keyword to it... and then use that short-code or keyword with some formatting like [[short-code=form1]] to the place where you want to show the form in admin site of CMS. and same form get reflects on front end site automatically. I want to achieve that automation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900