missing $args parameter for wordpress widget - php

I am actually learning wordpress plugin development by following a tutorial at code.tutsplus.com
At this point i m stucked as from where the function will get $args param in below function
function widget_coming_next($args) {
extract($args, EXTR_SKIP);
echo $before_widget;
list_upcoming_posts();
echo $after_widget;
}
the above function is simple wrapping by function list_upcoming_posts(); but still i don't see anywhere in the tutorial that this function is getting $args when its being called.
Below is how the function is called
function widget_coming_next_init() {
wp_register_sidebar_widget(COMING_NEXT_WIDGET_ID,
__('Coming Next'), 'widget_coming_next');
}
// Register widget to WordPress
add_action("plugins_loaded", "widget_coming_next_init");
Everything else is clear to me but i am unable to continue the lesson because of lack of understanding. Thank you all in advance.

The 3rd param of wp_register_sidebar_widget() is a callback function which is run when the widget is called. The callback function takes the form:
function my_output_callback_function( $args, $params ){ ... }
In your case, the callback function is your widget_coming_next() function. $args will be an array of various values.
Ref: http://codex.wordpress.org/Function_Reference/wp_register_sidebar_widget

Related

Wordpress admin_post callback not firing

This seems to be working on other sites I create however, my callback doesn't seem to fire. All I get is a white screen. Its like the callback does not exists.
I have checked has_action('admin_post_process_submission') and that returns true.
The plugin file itself returns other functionality absolutely fine.
I've been staring at JS for the last week or two so is there something I'm missing?
<?php class KnpvForm extends KnpvEndPoints {
function __construct() {
parent::__construct();
//Actions for processing the form <<< does not seem
add_action( 'admin_post_process_submission', array($this, 'knpv_admin_process_submission'));
//A filter which seems to work fine.
add_filter( 'tiny_mce_before_init', array($this, 'knpv_mce_butttons'), 10, 2 );
}
/**
* I expect this function to just print some words.
*/
public function knpv_admin_process_submission(){
echo '<pre>';
print_r('some words');
echo '</pre>';
}
} ?>

Disable Plugin Per Page - WordPress

I am working on a way to disable a specific plugin on a certain product page. I've cobbled this together from things I found online and the plugins code itself but its not working. Curious to have some fresh eyes have a look and let me know what might be failing. The post id of the product is 2679320. The actions I have set to remove are the ones referenced in the plugin wp_enqueue_scripts. Here is the code I'm trying by loading to snippets:
function remove__construct() {
global $post;
$ids = array(2679320);
if(in_array($post->ID,$ids)):
remove_action(‘wp_enqueue_scripts’,array($this,’enqueue_scripts’));
remove_action(‘plugins_loaded’,array($this,’load_txt_domain’),99);
remove_action(‘wp_footer’,array($this,’get_popup_markup’));
remove_filter( ‘pre_option_woocommerce_cart_redirect_after_add’, array($this,’prevent_cart_redirect’),10,1);
endif;
}
add_action(‘wp_head’, ‘remove__construct’, 1);
Any ideas why this isn't working? What did I miss? Anyone have better way to do this?
You can use Plugin Organizer. It allows you to selectively disable a plugin on a page or a complete post type.
There are 2 ways to disable plugin.
The first way is to create a custom plugin that removes the action that used to initialize your target plugin. The second way is to remove actions and filters which add scripts, styles and makes changes on a page.
Either way you choose, you have to remove actions after they've been added and before they actually worked. That means that for the first way in most cases you have to use plugins_loaded hook which can't be used in your functions.php (the first hook which can be used in functions.php is load_textdomain hook). in case you want to disable the plugin on certain pages you have to somehow get the current post ID, which isn't so easy because global $post variable is not available yet (The earliest hook with $post is wp).
Parameters for your remove_action depend on plugin add_action. The main point here is that all parameters of your remove_action must be the same as add_action parameters. Here are some examples :
add_action('plugins_loaded', 'init_function_name');
remove_action('plugins_loaded', 'init_function_name');
add_action('plugins_loaded', 'init_function_name', 100);
remove_action('plugins_loaded', 'init_function_name', 100);
class Plugin_Classname {
public static function init() {
add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ) );
}
}
remove_action( 'plugins_loaded', array( 'Plugin_Classname', 'on_init' ) );
class Plugin_Classname {
public function __construct(){
add_action('plugins_loaded', array($this, 'init'), 99);
}
public static function get_instance(){
if(self::$instance === null){
self::$instance = new self();
}
return self::$instance;
}
}
remove_action('plugins_loaded', array( Plugin_Classname::get_instance() , 'init'), 99);
Let's begin with the easiest way. Assume that removing scripts and styles is enough. Then you have to find wp_enqueue_scripts hooks in the plugin source. Eq.:
class Xoo_CP_Public{
protected static $instance = null;
public function __construct(){
add_action('plugins_loaded',array($this,'load_txt_domain'),99);
add_action('wp_enqueue_scripts',array($this,'enqueue_scripts'));
add_action('wp_footer',array($this,'get_popup_markup'));
add_filter( 'pre_option_woocommerce_cart_redirect_after_add', array($this,'prevent_cart_redirect'),10,1);
}
public static function get_instance(){
if(self::$instance === null){
self::$instance = new self();
}
return self::$instance;
}
}
As we need global $post variable we gonna use wp hook. Place the code below in functions.php:
function disable_plugin() {
global $post;
$ids = array( 2679320 ); // Disable plugin at page with ID = 2679320
if( in_array( $post->ID ,$ids ) ) {
remove_action('wp_enqueue_scripts',array( Xoo_CP_Public::get_instance(),'enqueue_scripts'));
remove_action('plugins_loaded',array(Xoo_CP_Public::get_instance(),'load_txt_domain'),99);
remove_action('wp_footer',array(Xoo_CP_Public::get_instance(),'get_popup_markup'));
remove_filter( 'pre_option_woocommerce_cart_redirect_after_add', array(Xoo_CP_Public::get_instance(),'prevent_cart_redirect'),10,1);
}
}
add_action( 'wp', 'disable_plugin' );
What if we want to remove an action is used to initialize this plugin? Let's take a look at add_action:
add_action('plugins_loaded','xoo_cp_rock_the_world');
In this case we can't use plugins_loaded hook because add_action is being called without priority parameter. If it's being called with priority parameter we could just create disable-plugin.php file in /wp-content/plugins folder and place this code there:
function disable_plugin() {
remove_action('plugins_loaded', 'xoo_cp_rock_the_world', 100);
}
add_action('plugins_loaded','disable_plugin');
But it's useless in this case without priority parameter. Yet we can cheat! We don't have to use any hooks and call remove_action directly. We should call it after target plugin add_action was called. Plugins are loaded in alphabetical order so if we named our plugin 'zzz-disable-plugin.php` with this lines of code:
/* Plugin Name: zzz-disable-plugin */
remove_action('plugins_loaded', 'xoo_cp_rock_the_world');
The target plugin will be disabled. At all pages though. I haven't find a way to get ID of current page on such an early hook. But we can use URI :
/* Plugin Name: zzz-disable-plugin */
if( 'product/polo' == trim( $_SERVER[ 'REQUEST_URI' ], '/' ) ) {
remove_action('plugins_loaded', 'xoo_cp_rock_the_world');
}

Wordpress get post id

I'm creating a plugin OOP for wordpress. The plugin creates a new custom post type called teams. Within a team page a could use the shortcode [program] to generate some predefault html code. Also i've created custom fields with new meta boxes.
The problem however is: when i'm entering the page thats calling the plugin, equal the team page with the sortcode i need to get post id within my plugin to retrieve get_post_meta().
I've tried the following things:
public function __construct(){
// not working
$post;
$post->ID;
// not working
global $wp_query;
$post_id = $wp_query->post->ID;
$post = get_post( $post_id );
// not workiing
echo '<pre>';
print_r('post_id:' . get_the_ID());
echo '</pre>';
}
How could i receive the custom post id within my plugin when i visited the page from frontend (so the plugin is called, runs the shortcode)
My main class gets loaded like this:
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
run_plugin();
Within MyPlugin the constructor looks like
public function __construct() {
if ( defined( 'PLUGIN_NAME_VERSION' ) ) {
$this->version = PLUGIN_NAME_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'MyPlugin';
if(!empty(get_option($this->plugin_name))){
$this->clientID = get_option($this->plugin_name)['client_id'];
}
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_shortcodes();
}
If your plugin constructor is getting called too early, the post data won't be set up and ready to use.
You'll need to hook into one of WPs actions that run after everything is ready. The init action should be enough for the post data, but depending on what else you need you can hook into wp_loaded, as it doesn't run until after WordPress is fully loaded.
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
/* run_plugin(); */ // <- instead of this....
add_action( 'wp_loaded','run_plugin' ); // <- ...do this
Try to define post as global like
global $post

Wordpress $wp_rewrite

I've a little question about $wp_rewrite object in Wordpress...
I'm building a plugin which need some routes were not included in Wordpress. So i'm trying to use the non_wp_rules action from $wp_rewrite object.
Here is my code :
add_filter('generate_rewrite_rules', 'add_rewrites');
add_filter('wp_loaded', 'flush_rules');
function flush_rules()
{
$rules = get_option('rewrite_rules');
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_rewrites()
{
global $wp_rewrite;
$rules = array(
'documentation/$' => 'fileToCall.php'
);
$wp_rewrite->non_wp_rules += $rules;
}
But I don't understand... My route is added with any problem (just a var_dump on $wp_rewrite is saying to me all is okay), but it seems the file i'm trying to call (fileToCall.php) which just contain an echo for moment (so no errors) returns a 404 error in Firefox dev console.
Is there someone who have this problem someday? Does I am doing something wrong?
Thanks for help.
wp_rewrite test if redirect address is locale or external. In your case it is external, so you have to use a different function. Just replace your second hook by this:
add_action('init', 'add_rewrites');
function add_rewrites()
{
global $wp_rewrite;
$wp_rewrite->add_external_rule('documentation/$', 'fileToCall.php');
}

Understanding how WordPress hooks are called

Here is how my current action hook example: it changes the meta description of the HTML page.
add_action( 'wp_head', 'add_a_description' );
function add_a_description () {
echo '<meta name="description" content=" This is new content" />' . "
";
} // End example
Now to me (I have some basic PHP knowledge, nothing more), it looks like the function is being called before it's been made. My intuition tells me that it should look like:
function add_a_description () {
echo '<meta name="description" content=" This is new content" />' . "
";}
add_action( 'wp_head', 'add_a_description' ); // End example
I understand this isn't the case, but I don't know why. Could someone explain it to me?
It does not matter where you write the action hook both will work:
add_action( 'wp_head', 'add_a_description' );
function add_a_description () {
echo '<meta name="description" content=" This is new content" />' . "";
}
and
function add_a_description () {
echo '<meta name="description" content=" This is new content" />' . "";
}
add_action( 'wp_head', 'add_a_description' ); // End example
These both will work. It does not matter where you declare the function, the only thing is that the function should be included in the script.
This is because the whole file is first parsed and then executed.
From PHP Manual
Functions need not be defined before they are referenced, except when a function is conditionally defined.
Regarding the wp_head action hook. The wp_head function resides inside wp-includes/general-template.php
If you look at that function it will call do_action('wp_head');
What this does is that, it will check for all actions and filters defined with wp_head hook which is stored in the global variable $wp_actions
If there is a hook in for wp_head it will call the hooked function using call_user_func_array
Hope this helps you :-)
Without knowing all of the inner workings, you can think of add_action() as simple function which passes the name of a function to be called at a certain point. In your code, add_action() lets the system know to call the function add_a_description() when it reaches the wp_head point.
Here's a VERY basic example of how the code works:
// Start of example system
$actions = array();
function add_action($point, $name) {
global $actions;
$actions[$point][] = $name;
}
function run($point) {
global $actions;
foreach ($actions[$point] as $name)
$name();
}
// End of example system
// Start of example plugin / theme function
add_action('wp_head', 'alert');
function alert() {
echo 'hello';
}
// End of example plugin / theme function
// At point wp_head
run('wp_head');

Categories