Wordpress: figuring out which hook called a function - wordpress

I'm trying to figure out if the same function is hooked onto multiple actions, can I figure out which action calls it?
I'd like to send out an API call when a user is created and deleted; the functionality in both cases is the same except one data point would be different based on if its created or deleted. It doesn't feel right making two identical functions with only one difference, but I'm not sure how else I can do it.
Advice?

That's the function current_filter():
add_action( 'plugins_loaded', 'common_action' );
add_action( 'admin_init', 'common_action' );
function common_action()
{
switch( current_filter() )
{
case 'plugins_loaded':
// do_something( 'Plugins loaded' );
break;
case 'admin_init':
// do_another_thing( 'Admin init' );
break;
}
}

I had the same question - when running a function, which action was triggering it? The key is $wp_current_filter.
Example:
// Both of these will call the same function
do_action('wp', 'my_function');
do_action('init', 'my_function');
function my_function() {
// How do I know if this was 'init', 'wp', or some other hook?
// Global in the WordPress variable $wp_filter
global $wp_current_filter;
if ($wp_current_filter == 'wp' || in_array('wp', $wp_current_filter)) {
// Do my "wp" based stuff....
}
if ($wp_current_filter == 'init' || in_array('init', $wp_current_filter)) {
// Do my "init" based stuff....
}
}
Note: This is a lousy use-case example, but it conveys the principles!

Related

admin_init hook not working as expected (cannot write to log, cannot add actions)

I'm trying to add some actions once after plugin activation. I found out that this should be done more or less like so:
register_activation_hook( __FILE__, 'activation_function' );
add_action('admin_init', 'after_activation_function');
function activation_function(){
add_option( 'activated_plugin_xyz', 'plugin xyz activated' ); //option is added to database
}
function after_activation_function(){
if (is_admin() && get_option ('activated_plugin_xyz') == 'plugin xyz activated'){
//do some things
wp_schedule_event(time(), 'daily', 'some_cron'); //cron event is added
add_action('wp_login', 'xyz_login_action');
add_action('some_cron', 'xyz_cron_job'); //cron job is correctly hooked
//delete_option('activated_plugin_xyz');
}
error_log("nothing in debug.log log...");
echo ("echoing works");
//wp_die('dying works');
}
function xyz_cron_job(){
error_log('cron job not logging anything...'); //nothing in log...
}
I can see the cron job in wp crontrol, the function xyz_cron_job is hooked, but when I trigger it manually nothing is written to the log. xyz_login_action also seems not to work.
If I do other things in xyz_cron_job they seem to have no effect as well...
after_activation_function is called bcs dying works if I uncomment it. Can anybody help me? It seems to me like I am missing something fundamental...
fixed it:
actions have to be added outside after_activation_function()
(I guess actions have to be hooked every time wp loads and if hooking happens in after_activation_function() the hook runs before the action is hooked)
working code looks like this:
register_activation_hook( __FILE__, 'activation_function' );
add_action('admin_init', 'after_activation_function');
//hook outside other functions
add_action('some_cron', 'xyz_cron_job');
add_action('wp_login', 'xyz_login_action');
function activation_function(){
add_option( 'activated_plugin_xyz', 'plugin xyz activated' ); //option is added to database
}
function after_activation_function(){
if (is_admin() && get_option ('activated_plugin_xyz') == 'plugin xyz activated'){
//do some things
wp_schedule_event(time(), 'daily', 'some_cron');
delete_option('activated_plugin_xyz');
}
}
function xyz_cron_job(){
error_log('logging works');
}

How to use conditional add_action for Wordpress

I am having a small issue where i need to call add_action method which is conditional. Basically here, first checking whether the plugin active or not by admin_init action. If true then i need another add_action call from that action.
Please find the scenario what i am looking for:
function check_some_other_plugin() {
if ( is_plugin_active('some-plugin.php') ) {
//if true then vc_before_init action will run
add_action( 'vc_before_init', 'vc_master_home_slider' );
}
}
add_action( 'admin_init', 'check_some_other_plugin' );
function vc_master_home_slider() {
//my stuff
}
I am really eager to know how this things can be possible. Thanks in advance.

How to show text in a page on Wordpress whithin a plugin

I am developing a plugin for a website.(Which is my first plugin for Wordpress)
The basic functionality is querying the database and in specific pages show the data from the database with a specific style instead of the content from the pages.
So far I managed to show some text in every specific page.
This is my code after some basic configurations:
global $wpdb;
global $wp_query;
add_action( 'wp', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I've read this post, but I couldn't solve my problem.
WordPress replace content of a page (plugin)
I already read the Wordpress documentation but I havent find anything there.
I found myself a solution, using shortcodes.
global $wpdb;
global $wp_query;
add_shortcode( 'sector_page_display', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function medicalPage()
{
return print "Medical";
}
See that instead of add_action I changed to add_shortcode
Then on everypage I will use to show info from the database I add
[sector_page_display]
in the page, so it call my method. You can add variables in there if you want.
You'll want to run that code before WordPress has fully loaded.
Try this
global $wpdb;
global $wp_query;
add_action( 'init', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I changed the add_action to now run the code when WordPress is being initialized.

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Custom post type functions.php if statement on action

I am using developing a child theme for Woothemes' Canvas.
I am trying to use functions.php in the child theme to only use actions on my custom post type.
This code doesn't seem to be working:
add_action( 'woo_post_inside_after', 'my_geo_mashup' );
function my_geo_mashup() {
echo GeoMashup::map();
if ($post->post_type == 'listings') {
//My function
}
}
add_action( 'woo_post_inside_before', 'listings_nivo' );
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
if ($post->post_type == 'listings') {
//My function
}
}
So, I'm unsure how to get the above to work properly and only show these items on the custom post type, or only for the custom post type template single-listings.php (as I only want the map and slider to show on the actual post, not on the blog page (archive.php)
Rather than making the entire $post object global, you can just make $post_type global instead. Ex below.
I'm not exactly sure where that function is being loaded, but make sure you hook somewhere within the post. If the action is before, as far as I know and from experience, the post variable will be null.
Just as a test, try running the action in wp_footer Ex. add_action( 'wp_footer', 'listings_nivo' );
See if that yeilds any results.
if echoing var_dump($post) is still null, well, not sure where to go from there.
So you can try running the below, then run the action in the appropriate place if it works:
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
global $post_type;
// Diagnostic purposes
echo var_dump($post_type);
if ($post_type == 'listings') {
//My function
}
}
add_action( 'wp_footer', 'listings_nivo' );
Check your error log or turn wp_debug to true in your wp-config.php file if nothing else to see if anything else is going on.
Best of luck!
Inside your function, try adding global $post;. Then to see what you are getting with $post->post_type echo it out to the screen. As long as this gives you "listings", your code should work. If not, there's probably another issue at play.

Resources