0

we are trying to create a basic system for us. Where we want to create shortcode like [gallery id="1"] we have created this way of shortcodes.

But when it comes between content like:

<h1>Some Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer purus purus, scelerisque ut ex non, pharetra interdum tellus. Aenean sed libero a ipsum vestibulum feugiat quis quis magna. Mauris a condimentum mi, et feugiat leo.</p>
[gallery id="1"]

This is how my template looks like from editor. How can i read the perticular [gallery id="1"] and make it a function like this

gallery(1).

Is there any way i can do this? What i am using here is WYSIWYG editor and data is saved in MySQL database.

1
  • 1
    are you using any php framework? Commented May 7, 2015 at 7:14

3 Answers 3

1

a beginning of answer : you have to use regular expression like this :
#\[gallery id\=\"([0-9]+)\"\]#
this will get all the [gallery id="XXX"], and the () will allow you to catch the actual number, and then call your gallery(XXX) function

Sign up to request clarification or add additional context in comments.

3 Comments

Ok but how can i make it a function? will there be any way i can make it a PHP tag?
you said we have created this way of shortcodes, what have you done about this short code ? have you made the gallery(int $id) function ? and then, all you need is to call that function ? I don't know how Word Press Plugins use theses tags (but I've already used them). In my case, the plugin I used must be a JS plugin, since it seems to add elements after the DOM is created... But I don't know how you can call a PHP function from JS
Yes we just have to call the function. We have already created these functions.
0

I created a framework-agnostic library to support [shortcodes] in pure PHP projects: https://github.com/thunderer/Shortcode . You just need to create library instance, register name handlers for shortcodes you wish to use and you're good to go.

Your case will be solved like this (I took this example from README):

// put those in the use block on top of your file:
use Thunder\Shortcode\Extractor;
use Thunder\Shortcode\Parser;
use Thunder\Shortcode\Processor;
use Thunder\Shortcode\Shortcode;

// code below does the magic:
$processor = new Processor(new Extractor(), new Parser());
$processor->addHandler('gallery', function(Shortcode $s) {    
    // return here any HTML code you need

    return $s->getParameter('id'); // this will return "id" parameter
});

$processor->process('[gallery id="2"]'); // will return "2"

Comments

0

I think RegEx Patterns should work. Sorry I'm not that good at RegEx, but here's my pattern:

http://regexr.com/3auv2

1 Comment

Thank you for the efforts. And yes it will solve it. But i want to convert that to <?php gallery(1) ?> any solution for that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.