add_action with a class method is causing fatal errors
Try: <?php $class = new MyClass(); class MyClass { public function MyClass() { add_action(‘init’, array($this, ‘core’)); } public static function core() { echo “I never get to here”; } } ?>
Try: <?php $class = new MyClass(); class MyClass { public function MyClass() { add_action(‘init’, array($this, ‘core’)); } public static function core() { echo “I never get to here”; } } ?>
This will get you most of the way there, but the actual search is not perfect. It would be great if someone who knows more about $wpdb can help get a better result. If this is my original post title: SOPA is dead, woot! Then just a title of “SOPA” will find it, as will … Read more
You’re on the right track with creating the plugin. All your assumptions are correct. To avoid errors on activation you’ll want to wrap the functions that you are redefining in function_exists blocks, as on activation those functions will already be defined: if ( ! function_exists( ‘wp_hash_password’ ) ) : function wp_hash_password( $password ) { return … Read more
There’re two very often forgotten action ref arrays: loop_start/_end(). Just turn on output buffering and you’re ready to go. add_action( ‘loop_start’, ‘wpse75307_plugin_loop’ ); add_action( ‘loop_end’, ‘wpse75307_plugin_loop’ ); /** * Callback function triggered during: * + ‘loop_start’/`have_posts()` after the last post gets rendered * + ‘loop_end’/`the_post()` before the 1st post gets rendered * @param object \WP_Query … Read more
Include your template files in your plugin. I stick mine in /plugin/templates. You need to hook into template location for that template: add_filter(‘archive_template’, ‘yourplugin_get_custom_archive_template’); function yourplugin_get_custom_archive_template($template) { global $wp_query; if (is_post_type_archive(‘yourCPT’)) { $templates[] = ‘archive-yourCPT.php’; $template = yourplugin_locate_plugin_template($templates); } return $template; } Rinse and repeat with the appropriate checks for each template you want to … Read more
In your example, there is currently no difference. You get the same object, if there is one. Try it: global $current_screen; $current_screen->foo = 1; $screen = get_current_screen(); $screen->foo = 2; echo ‘$current_screen->foo: ‘ . $current_screen->foo; // 2! The simple reason: objects are not passed as a copy in PHP. But: global variables are really bad, … Read more
WordPress itself is not very object oriented – it has objects (like WP_User, for example), but most interactions with the core API take place through plain function calls. It also depends widely on global state, though it does a good job of managing this through the abstractions of actions and filters. In my experience there … Read more
Give it a try like so: File: oowp/bootstrap.php <?php /* Plugin name: OOWP */ require_once( ‘autoload.php’ ); use oowp\objects\Custom_Objects; new Custom_Objects;
The earliest safe hook to get post information is the template_redirect hook. All the hooks in question runs before WordPress has setup postdata, so any post info are still unavailable at that point. The globals like $wp_query and $post will still contain no data, that is why your efforts returns nothing. EDIT Extra info as … Read more
There is no regular output and hence no header sent before template_redirect on the front end. If you need sessions on the back end too, use the action wp_loaded to cover both. Example: add_action( ‘template_redirect’, function() { $status = session_status(); if ( PHP_SESSION_DISABLED === $status ) { // That’s why you cannot rely on sessions! … Read more