Setting post_id for single.php based on URL without a redirect

I figured it out, here is the solution for anyone else wondering how to do something like this: function force_ID($query) { global $wpdb; if (substr($_SERVER[‘REQUEST_URI’],-5) == ‘.html’) { $post_id = $wpdb->get_var($wpdb->prepare(“SELECT ID FROM $wpdb->posts WHERE post_name=%s AND post_status=”publish””,substr(basename($_SERVER[‘REQUEST_URI’]),0,-5))); if ($post_id > 0) $query->set(‘page_id’,$post_id); } } add_action(‘pre_get_posts’,’force_ID’); In examining the wp-includes/query.php file, I noticed that the … Read more

How can I get more out of `$post` in an `add_action` callback?

Simply use the post ID, provided by $post_id (and stored at $post->ID) to use the various functions WordPress provides to get any extra data related to that post (get_the_terms,get_post_meta etc). Performance wise you suffer no loss by using these functions as opposed to trying to force more data into the $post object (probably the reverse … Read more

Running a function with args in add_action()

No, this cannot work. Haven’t you tested your idea? A simple workaround would be a class: class WPSE_53453_Action_Handler { public $tokens, $payloads; public function process() { $tokens = $this->tokens; $payloads = $this->payloads; // do something awesome } } $WPSE_53453 = new WPSE_53453_Action_Handler; $WPSE_53453->tokens = array ( ‘foo’, ‘bar’ ); $WPSE_53453->payloads=”hello”; add_action( ‘publish_post’, array ( $WPSE_53453, … Read more

update_user_meta not updating

In response to the extended question, i.e. Another question is[…]: It is worth noting that WordPress’ plugin API does not persist between requests – that is to say, any functions associated with actions through WordPress’ add_action() are only associated for the duration of the script’s execution. Each subsequent request served by WordPress must once again … Read more