Wordpress admin_post callback not firing - php

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>';
}
} ?>

Related

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');
}

Add some content to woocommerce product single page via a plugin

I'm somewhat new to developing plugins for Wordpress and I have this idea for a simple woocommerce based plugin. I need this plugin to do something only on single product pages but I can't see to figure out at all how I would add an action when a product gets loaded to do something.
Does anyone have any experience with this?
In my plugin I've attempted to just get this working with some code
public function get_product_variation_data() {
function get_data() {
echo '<script>';
echo "console.log('beep');";
echo '</script>';
}
add_action( 'woocommerce_after_single_product', 'get_data' );
}
But this doesn't seem to run at all on my product page. Maybe there is a different hook I need to attach to?
Try this :
class PluginTest {
public function __construct(){
add_action( 'woocommerce_after_single_product', array($this, 'get_product_variation_data'));
}
public function get_product_variation_data(){
echo '<script>';
echo "console.log('beep');";
echo '</script>';
}
}
$plugin_test = new PluginTest();
You need to add in your plugin constructor __construct() the following:
add_action( 'woocommerce_after_single_product', array( $this, 'get_product_variation_data' ), 5 );
Then outside the constructor:
public function get_product_variation_data() {
?>
<script> console.log('beep'); </script>
<?php
}
This should work…

missing $args parameter for wordpress widget

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

How to define and use helper functions in theme functions.php?

How to get following to work properly in Wordress Theme functions.php file?
I haven't figured out how to make the top function available to the bottom function within the theme's functions.php file. I'm not grasping how to setup hooks so they can work together. Thank you.
Filter/helper/whateveryoucallit function:
function poohToPee( $pooh_log )
{
switch( $pooh_log )
{
case 'gross-poop':
$pee_equivalent = 'Grossest of Pees';
break;
case 'ok-poop':
$pee_equivalent = 'Bland Snack Pee';
break;
case 'shang-tsung-plop':
$pee-equivalent = 'Random U-Stream';
break;
}
return $pee_equivalent;
}
Ajax handler function:
function screw_loose()
{
if( isset($_REQUEST['pooh_log']) )
{
echo poohToPee( $_REQUEST['pooh_log'] );
}
}
add_action('wp_ajax_priv_screw_loose', 'screw_loose')
The add_action usually calls the function you are passing through at the point that hook is called.
Since you are using some sort of ajax hook are you really able to make sure your function isn't being called? It wouldn't be echo-ing anything out to the screen since it is running in the background.
Normally any function you define in functions.php is readily available to use within the theme.
It is obviously normally best to organize and have classes, in which case you'd pass the method to the hook as an array, for example add_action( 'admin_init', array( $this, 'someFunction' ) ); and that add_action I just did would be put in the __construct function of the class.
For instance you could do this:
class helloWorld
{
function __construct()
{
add_action( 'admin_init', array( $this, 'echoItOut' ) );
}
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
Alternatively you could also do this:
class helloWorld
{
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
add_action( 'admin_init', array( $helloWorld, 'echoItOut' ) );
Or simply:
function echoItOut()
{
echo 'Hello World';
}
add_action( 'admin_init', 'echoItOut' );
If you put any of these blocks of code I provided in your functions.php file and go to your Dashboard you will see 'Hello World' printed out at the top under the admin bar most likely (might vary from theme to theme if the dashboard has custom styling).

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