Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Virtual" slugs?
#1
I've worked with a different CMS and it has an interesting feature that I was trying to determine how to duplicate in GS. (I'm translating terms into GS speak so hopefully this ends up clear).

Lets say I want to create a plugin that creates a page of content.

To do this currently, as far as I know, I need to create a page, then on the page put in a plugin hook using some short code like (% whatever %). My plugin needs a add_filter hook and does a massage of $content to replace the code with the desired content.

This means my plugin parse gets called every page.

I'd like to instead have my plugin "register" a slug with GS, such that when the url is to that slug, a function in my plugin gets called to return the page content. Of course, the plugin configuration would also require picking a template to render the page with...or use the default template...?

This would allow a page to be generated with much less overhead on all the other pages.

Any suggestions/thoughts on this idea? If it worthwhile and how it could be implemented? (In the other software I used they call this feature "custom perma links".)

-Rob A>
Reply
#2
RobA Wrote:Lets say I want to create a plugin that creates a page of content.

To do this currently, as far as I know, I need to create a page, then on the page put in a plugin hook using some short code like (% whatever %). My plugin needs a add_filter hook and does a massage of $content to replace the code with the desired content.

This means my plugin parse gets called every page.

I'd like to instead have my plugin "register" a slug with GS, such that when the url is to that slug, a function in my plugin gets called to return the page content. Of course, the plugin configuration would also require picking a template to render the page with...or use the default template...?

This would allow a page to be generated with much less overhead on all the other pages.

Any suggestions/thoughts on this idea? If it worthwhile and how it could be implemented? (In the other software I used they call this feature "custom perma links".)

I would still create the page in order to have keywords, set the template, set the position in the menu (if needed), etc.

Then check for the slug configured in your plugin either before adding the filter
Code:
if (@$_GET['id'] == myplugin_get_slug_from_config()) {
  add_filter('content','myplugin_filter');
}

function myplugin_filter($content) {
  $content = ... // plugin special content, actual content of page is ignored
}
or in the filter
Code:
add_filter('content','myplugin_filter');

function myplugin_filter($content) {
  global $id;
  if ($id == myplugin_get_slug_from_config()) {
    $content = ... // plugin special content, actual content of page is ignored
  }
}

Even if there was a possibility to "register" a slug in GetSimple, the execution would not be more efficient.

(Corrected code, as $id is not available at startup of the plugin)
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#3
Just thinking aloud here: Couldn't you use a check with return_page_slug(), sort of like this:

http://get-simple.info/forum/post/14808/#p14808
- Chris
Thanks for using GetSimple! - Download

Please do not email me directly for help regarding GetSimple. Please post all your questions/problems in the forum!
Reply
#4
ccagle8 Wrote:Just thinking aloud here: Couldn't you use a check with return_page_slug(), sort of like this:

Within the filter you can do this (it's equivalent to checking the global $id variable), as it is always called from the frontend. Outside you would have to test first, if it is the frontend, e.g. if (function_exists('return_page_slug')) ...
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#5
RobA Wrote:I need to […] put in a plugin hook using some short code like (% whatever %).
No you don’t.
Code:
add_action('index-pretemplate','changeContent');
function changeContent() {
    global $content;
    $content = 'My plugin content!';
}
Will use “My plugin content!” as the page’s content instead of whatever content is saved in the XML file.
RobA Wrote:This means my plugin parse gets called every page.
Not necessarily. When your plugin file is being loaded the current page slug is already accessible.

Normally the slug is figured out with the following code (from index.php):
Code:
if (isset($_GET['id'])){
    $id = str_replace ('..','',$_GET['id']);
    $id = str_replace ('/','',$id);
    $id = lowercase($id);
} else {
    $id = "index";
}
Using this and the previous content overwrite in your plugin file you could easily build something like:
Code:
if (isset($_GET['id'])){
    $id = str_replace ('..','',$_GET['id']);
    $id = str_replace ('/','',$id);
    $id = lowercase($id);
} else {
    $id = "index";
}
if ($id=='mypluginslug') {
    add_action('index-pretemplate','changeContent');
    function changeContent() {
        global $content;
        $content = 'My plugin content!';
    }
}
Now this action will only be used when the current page has the slug “mypluginslug”. You can exchange the add_action() for add_filter() if that’s what you want to use of course.

ccagle8 Wrote:Couldn’t you use a check with return_page_slug()
return_page_slug() is not available to plugins until inside the index-pretemplate hook. So if you are concerned about adding a filter to pages where the filter should not be executed that won’t work. (Or it will, but it will be a very roundabout kind of way.)
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#6
Zegnåt Wrote:
RobA Wrote:I need to […] put in a plugin hook using some short code like (% whatever %).
No you don’t.
Code:
add_action('index-pretemplate','changeContent');
function changeContent() {
    global $content;
    $content = 'My plugin content!';
}
...

@Zegnåt - so that code and this:

Code:
add_filter('content','changeContent');
function changeContent($contents) {
  $my_content = 'My plugin content!';
  return $my_content;
}

do the exact same thing? Which is preferred then?

@all - Some good suggestions in general! Thanks. It still seems like a lot of steps to do one thing. Look at the simple case of a contact page. To make a contact page you need to:
1) install the contact plugin.
2) configure the plugin.
3) create new contact page, making sure you add it to the menu, and add the correct plugin code to the page.

and the plugin code will be executed for every page (be default) unless the steps suggested were followed.

Even with those suggestions, it seems odd to have to create a page (whose content is always overwritten) in order to work.

I guess I was thinking of a something in the 404 handler to check "nope, not a real (xml) page", then check to see if any plugins have registered to have a certain slug, and call the registered function to generate content using the default page template, to avoid the need to create a page.

-Rob A>
Reply
#7
RobA Wrote:Which is preferred then?

The difference is a subtle one and has to do with compatibility. If you want your plugin to support other plugins that use filters — maybe to add certain things in the beginning or the end of the content — you cannot use a filter yourself.

If you use a filter to overwrite the content variable, all previously run filters will have their effects overwritten. By overwriting the content variable before the filters are applied all will stay in effect.

There are also places, such as certain plugins or GetSimple’s own get_header(), that will use $content without running a filter. Again, overwriting the content variable as soon as you can will make sure these are supported.

RobA Wrote:I guess I was thinking of a something in the 404 handler to check "nope, not a real (xml) page", then check to see if any plugins have registered to have a certain slug, and call the registered function to generate content using the default page template, to avoid the need to create a page.

This is probably possible right now. You can let your plugin create an XML file that fakes the one used by pages somewhere on the server. Then run something like the following (untested) code:
Code:
add_action('error-404','notFOF');
function notFOF() {
    global $file, $id;
    if ($id=='contact') {
        $file = GSDATAOTHERPATH.'myplugin/blank.xml';
        add_filter('content','changeContent');
        function changeContent($contents) {
            $my_content = 'My plugin content!';
            return $my_content;
        }
    }
}
Now our fake slug “contact” gets assigned a dummy-file. This is to make sure GetSimple doesn’t give up when it does getXML() on $file. It is also important that we get GetSimple to recognise an URL other than “404” to keep it from sending the Not Found header to the UA. We also add the content rewriting function in there for good merits. Your plugin could, of course, just plant an XML file with the correct content to the server already but creating the content through PHP allows us to use the same dummy XML file for all “virtual pages”.

After this you will still need to add code into the changedata hook to make sure nobody creates a page using your virtual slug. That might even be the hardest part… maybe I’ll look into that tomorrow.

You are right about this being build in to the core would make development way easier!

It’s getting late and I’m tired so I hope I’m still making sense ;-)
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply




Users browsing this thread: 1 Guest(s)