• Resolved Demorden

    (@demorden)


    Hello and thanks for the awesomeness. πŸ™‚
    I’ve tried this plugin on a site of mine and it seems to help quite a lot.

      Can you confirm it also works with custom post types?

    Also, it would be great if you could insert per-page options about which content to prerender.

    I have limited coding experience, so I’ll just add comments to the code snippet you recently shared, to show what I’m thinking about.

    add_filter('wpinstant_prerendered_urls', function($urls) {
    
    // CHANGE "IF HOME" TO "IF FIELD IS SET"
            if ( is_front_page() ) {
    // GET THE POSTS IDS ARRAY FROM PREVIOUSLY SET CF
    		$posts = array( 1,2,3);
    // THE REST COULD STAY UNTOUCHED, I SUPPOSE
    		foreach($posts as $post) {
    			$permalink = get_permalink( $post );
    			if($permalink) {
    				$urls[] = $permalink;
    			}
    		}
    	}
    	return $urls;
    });

    That kind of setting would override the default settings in every page, right?
    I’m trying something with ACF, but if you could make it work out-of-the-box it could be a great feature.

    https://wordpress.org/plugins/instant-articles/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter Demorden

    (@demorden)

    Ok, I’ve seen it works with CPT.
    It had to, after all. πŸ™‚

    Now to the serious things. πŸ˜›
    Just so that you know, I’ve tested my idea as exposed above, and I rewrote your snippet into this:

    add_filter('wpinstant_prerendered_urls', function($urls) {
        $values = get_field('articolo_prerender');
            if($values) {
                foreach($values as $value) {
                    $permalink = get_permalink( $value );
                    if($permalink) {
                        $urls[] = $permalink;
                    }
                }
            }
    return $urls;
    });

    (reference: http://www.advancedcustomfields.com/resources/code-examples/#working-with%20array%20values )

    On my posts I set up a field with ACF – a post-object outputting the selected posts IDs.
    Well, it works just fine.

    BUT this setup just ADDS posts to the prerendering list, whilst my goal was to OVERRIDE the default settings completely.

    Any help on that?
    Thanks a lot. πŸ™‚

    Plugin Author Stanislav Khromov

    (@khromov)

    Hey Demorden,

    Glad you got it working!

    You raise a great point about the prerender filter not letting you override URLs. We have a new version coming out in the coming days and I’ll make sure to tweak the filter to handle that use case.

    We’re trying to make prerendering as hands-off as possible, but pre-rendering per page through a UI makes sense, we’ll put that on the todo list.

    I’ll ping back as soon as the filter tweaks are released.

    Thread Starter Demorden

    (@demorden)

    Thanks for the quick answer.

    If I understand correctly, you are saying that with the upcoming version, if I use a filter like the one we discussed, it will override the prerendering rules, rather than being an addition?

    (Just to make it crystal-clear: what I’d like to achieve is that if I set “post1” and “post5” as prerendered pages, all other rules are ignored. For pages with no such settings, the default rules are applied.)

    Very cool! πŸ™‚

    I understand that a built-in GUI to override the rules is not a priority.
    If the filter will work as intended, it would be more than enough for now.

    I’m leaving 4 stars, waiting for the new version.
    I’m sure I’ll have to change my rating to 5 stars, for this reason – and other improvements as well. πŸ™‚

    Keep on with the good work! ^_^

    PS: I’ll set this as solved as soon as the filter will be able to completely override the rules, but I’m set for now. πŸ™‚

    Plugin Author Stanislav Khromov

    (@khromov)

    Hey Demorden,

    Just pushed version 1.4 which lets you override which links are prerendered through the wpinstant_prerendered_urls filter. You can just reinitialize the $urls array in your example, something like:

    add_filter('wpinstant_prerendered_urls', function($urls) {
        $urls = array();
        //...
        return $urls;
    });

    However, this will still incur the database calls to find latest, adjacent or sticky posts (if you have those enabled under Admin > Instant Articels). To avoid this extra overhead, you can drop this line into your code as well to disable that:

    add_filter('wpinstant_prerendered_urls_override_defaults', '__return_true');

    (Note that if you run this on every page load, your Prerender settings in the admin panel will not be respected!)

    Let me know if you have any issues with this!

    Thread Starter Demorden

    (@demorden)

    It works like a charm! ^_^
    Simply awesome.

    Now I’ll add some code to decide per-post whether to ADD rules or to OVERRIDE them.
    Then I will really feel ready to use it in production.

    Thanks, I’m changing my rating to 5 stars. πŸ™‚
    Have a nice day! πŸ™‚

    Thread Starter Demorden

    (@demorden)

    Hello again Stanislav, since I’m starting to save (and share) my snippets on Github, I took the liberty to include the one I used for your plugin. πŸ™‚

    It’s as simple as you could imagine, but it works, and could be useful to someone else as well.

    I saved it as a plugin and made it compatible with Github Updater, so that anyone could easily use it if needed.

    https://github.com/Demorden/prerendering-rules-for-wp-instant-articles

    I would love if you could take 5 minutes to review it; I am mainly concerned with giving the correct credits and licenses.

    Thanks for all πŸ™‚

    Plugin Author Stanislav Khromov

    (@khromov)

    Hey Demorden,

    Cool plugin!

    What you could do is to export your field group to PHP (via the ACF > Tools menu) and then include that export code in your plugin. That way the field group will be generated automatically if the user has ACF installed.

    No worries about the licenses. πŸ™‚

    Thread Starter Demorden

    (@demorden)

    Oh, great tip, thanks.
    I’ll do it ASAP. πŸ™‚

    Have a nice day/evening/whatever-it-is-in-your-time-zone. πŸ™‚

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Per-page prerendering options suggestion’ is closed to new replies.