WordPress.org

Ready to get started?Download WordPress

Forums

Converting a page template to a plugin (12 posts)

  1. nick_nielsen
    Member
    Posted 7 months ago #

    Context :
    I have decided to stop using the NextGen gallery plugin - basically because the code has bloated from 1Mb to 9Mb with features I don't want (as it was when Alex Rabe maintained it, I only used the basic image management). It was rather forced upon me as when I tried to follow the instructions to wind back to the pre-version 2, it broke the site and I lost all the image information.
    So I have decided to use WordPress' media management (which was inexistant when I started using NextGen). I've found a plugin to deal with the thumbnail cropping (because what's on offer is useless) and now I'm working on creating a way of presenting the images.
    I've looked at various "portfolio" plugins, but none of them actually offer a solution for what I need - they are oriented towards graphics people or photographers and I'm looking for a way of presenting my work as a visual artist.

    Bearing in mind Justin Tadlock's advice to put personal "permanent" customisations into a plugin rather than changing functions.php every time you change or modiy your theme, I'm trying to do this with my galleries.

    My solution at the moment is a custom page template, but it would be better if the gallery could be called with a shortcode

    Problem:
    I use the content on the page to give an introduction to the works that are being shown and this needs to be integrated into the gallery structure -
    schematically the existing custom template looks like this :

    < Query to get the attached images >
    < Do things >
    < the_content() --intro to work >
    < do the rest >

    This works. But trying to get to the shortcode solution has defeated me. I need to stop the_content() from printing as is usual, and I need it's output as a variable.

    I've found that I can get something using $post->post_content but it starts, inevitably with "[workgallery]..." my shortcode, which I don't know how to get rid of and equally inevitably, the_content() outputs below as well.

    I trawled the net, and found that I could put the_content() into an output buffer, which would give me it's output as a variable but as the content of the page contains my shortcode, it sets up an infinite loop and the page goes into meltdown.

    I've looked at add_filter(the_content, $myvariable) but all the examples I've found have been for adding bits to a post, rather than manipulating the output as described above.

    Advice and possible solutions would be much appreciated.

  2. clhereistian
    Member
    Posted 7 months ago #

    Are you just trying to remove the shortcode from your post content? If so,
    there is strip_shortcodes().

    $content = strip_shortcodes($post=>post_content);

    Or you can do it easily with php:

    $content = str_replace("[workgallery]", "", $post->post_content);

    or

    $content = preg_replace("/[workgallery]/", "", $post->post_content);

  3. nick_nielsen
    Member
    Posted 7 months ago #

    Thanks for this.
    It's half of what I'm trying to do !

    It still leaves me with the problem of stopping the_content() from printing out after the gallery as well.

    I think what I am trying to do (if it is indeed the right way to go) is to find a way of getting a template part into the page which the_content() sort of slots into...

    I've just realised I also want to find out a way to turn off post thumbnails / featured images on particular pages - but that's another question...

  4. bcworkz
    Member
    Posted 7 months ago #

    Just set $post->post_content to what you actually want to be output when the template calls the_content(), even if it's an empty string. That's a rather hacky way to do it though. Better to hook the filter 'the_content' and change it that way. It does the same thing, but filters are the "proper" way.

    Having a personal plugin is great advice, but if you find the need to alter what templates are doing, altering by plugin can be difficult. For custom templates safe from theme updates, create a child theme.

  5. nick_nielsen
    Member
    Posted 7 months ago #

    I'm actually working from within a child theme - what makes me try and go the plugin route is that with my site effectively being down I need to get my galleries back online as quick as possible and this seems a good way to go, as it is one of the core functions of my site.

    I'll see if I can use a filter hook... something I've never done.
    One of the beauties of the old NextGen was that it was so easy to hack. This is considerably more difficult.

  6. nick_nielsen
    Member
    Posted 7 months ago #

    I don't see how you can use a filter hook in this case - as filters modify the content of every post this is only for specific instances.

  7. clhereistian
    Member
    Posted 7 months ago #

    I am having trouble following what you are trying to do.

    So are you saying that you need to separate the shortcode from the content, place the shortcode where you want without the content, and and place the content where you want without the shortcode?

    If so, you already have the content without the shortcode, and if you want to strip out the shortcodes, you can use this:

    http://codex.wordpress.org/Function_Reference/get_shortcode_regex

    If that isn't what you need, please explain.

  8. nick_nielsen
    Member
    Posted 7 months ago #

    What I'm trying to do is use a default page template to output my gallery.

    At the moment I can

    • output the gallery
    • thanks to you, retrieve $post->post_content and get it to show without [shortcode] showing in it

    and it all works

    but
    the output generated by the_content() still prints out below the working gallery.
    I need to turn this off.

    When I was using NextGen (I discovered yesterday that someone has begun maintaining Alex Rabe's version, but too late for me) everything about the gallery, introduction, description of images etc.I did in NexGen and the design and markup of the gallery was done by hacking a NextGen template. A gallery page simply consisted of [nggallery] - no text because everything was already in the gallery.
    This meant that, apart from some css styling, my galleries were seperate from anything to do with theme development. Whatever the markup was for a page, the gallery just dropped into it.
    I'm trying to get back to this state.

  9. clhereistian
    Member
    Posted 7 months ago #

    Can you post the code you are using?

  10. bcworkz
    Member
    Posted 7 months ago #

    If you're directly working on the template, either delete the call to the_content() or use some sort of conditional like if ( 123 != the_ID()) the_content(); where 123 would be replaced with your actual gallery page ID.

    Or consider creating a custom page template for you gallery, leaving the default for other purposes.

  11. nick_nielsen
    Member
    Posted 7 months ago #

    This conversation is getting oddly circular.
    Thank you all for your contributions - I think I've got to find another way to approach the problem, perhaps a custom post type or trying to find a different hook, I think the_content() is too late in the process.

    I may post here if I find an interesting solution.

  12. nick_nielsen
    Member
    Posted 7 months ago #

    My solution has been to create a custom field called "exhibition" which I set to "true" in gallery posts
    in functions.php I have added this code

    add_filter('the_content', 'wgallery');
    function wgallery( $content )
    {
        $postid = get_the_ID();
        $gallery_switch = get_post_meta( $postid, 'exhibition',true );
        if ($gallery_switch == 'true') {
            $gallery_content = work_gallery($content); /*function which gets attached images and arranges them in the gallery markup*/
            return $gallery_content;
        } else {
            return $content;
        }
    }

    This works , amazingly enough. it does make the gallery completely independent of any markup in the theme.
    Now I have to find a way of turning of post thumbnails on the gallery pages - as there are already enough pictures !

Reply

You must log in to post.

About this Topic