• Resolved tolisp

    (@tolisp)


    Hello,
    thank you for your plugin. I am trying to integrate the plugin on my theme so that i can purge a specific post (or array of posts) on an event invoked by an action in my theme. I saw the varnish_http_purge_events filter, but i want to invoke the purge myself and not include custom events on the default ones. Is there a way to invoke the purge_post function from the theme or do you recommend another way?

    Thanks
    Tolis

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    I am trying to integrate the plugin on my theme so that i can purge a specific post (or array of posts) on an event invoked by an action in my theme.

    Depends on the action.

    I’m assuming you read about https://github.com/Ipstenu/varnish-http-purge/wiki/Custom-Filters#add-event-to-force-a-purge already

    If you’re flushing the exact same pages every time, that function would be what you want. Add in the event, and then in your function tell it to flush.

    All you really have to do is:

    1. Collect the posts you want to flush
    2. On that action from your theme, tell it to flush those posts

    I personally do it with a weirdly complex “If a post in post-type A is being edited, trigger a flush on all related posts in post-type B.”

    But the specifics depend on .. well .. the specifics ??

    Thread Starter tolisp

    (@tolisp)

    Thanks for the reply Mika. I am actually trying to purge all custom posts that have a specific term in a custom tax when the the term changes.

    tell it to flush those posts

    So here is where you instantiate the plugin’s class and call the purge_post function right?

    • This reply was modified 2 years, 2 months ago by tolisp.
    Plugin Contributor Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Sorry for the delay, I’m getting over Covid.

    I am actually trying to purge all custom posts that have a specific term in a custom tax when the the term changes.

    Ah, that’s not terribly hard.

    You want to hook into the term editor or just say “Any time a term is edited, flush the site”

    The latter is ‘faster’ to code:

    function my_varnish_purge_events( $actions ) {
    	$myactions = array(
    		'edit_term',	// When a term is edited
    	);
    	array_merge( $actions, $myactions ) ;
    	return $actions;
    }
    add_filter( 'varnish_http_purge_events', 'my_varnish_purge_events' );
    add_filter( 'varnish_http_purge_events_full', 'my_varnish_purge_events' );
    

    The double filters are because I had to put in some odd redundancy.

    The former would mean hooking into edit_term :

    function my_term_edit( $term_id, $tt_id, $taxonomy ){
        // do some stuff
    }
    add_action( 'edit_term', 'my_term_edit', 10, 3 );
    

    And then having code that

    1. Gets a list of all posts in that term
    2. Add all the posts by URL to be purged

    That second step is probably going to be the biggest headache. If you look at the wp-cli code for the plugin – https://github.com/Ipstenu/varnish-http-purge/blob/trunk/wp-cli.php – you’ll see there’s a __construct() to create a connection to VarnishPurger(), and that allows you to call things like:

    $this->varnish_purge->purge_url( $url );

    More or less that’ll be what you want to duplicate.

    Thread Starter tolisp

    (@tolisp)

    Oh sorry to hear that, hope you get over it fast ??
    Thanks for the help, i appreciate it!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Purge post by ID on demand’ is closed to new replies.