plugin add action hook is not working : - wordpress

I am trying to add an custom action hook in wordpress but it's not working.Please help me through this.
<?php
function wp_add_google_link(){
global $WP_Admin_Bar;
var_dump($WP_Admin_Bar);
$WP_Admin_Bar->add_menu(array(
'id'=>'google_analytics',
'title'=>'GoogleAnalytics',
'href'=>'https://google.com/analytics'
));
}
add_action('wp_before_admin_bar_render','wp_add_google_link');

I guess it is working but returning NULL value with an error. It is because wp_admin_bar should be lower case. But you are using WP_Admin_Bar.
Please check the reference:
https://codex.wordpress.org/Class_Reference/WP_Admin_Bar
and
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_before_admin_bar_render
function wp_add_google_link(){
global $wp_admin_bar;
var_dump($wp_admin_bar);
$wp_admin_bar->add_menu(array(
'id'=>'google_analytics',
'title'=>'GoogleAnalytics',
'href'=>'https://google.com/analytics'
));
}
add_action('wp_before_admin_bar_render','wp_add_google_link');
Screenshot of result

Related

Customize woocommerce product name hook

The reason behind doing this is because I am currently had set my default product name hook with a custom function and I am currently trying to set another custom hook to avoid conflict with my default hook. This is what I have here for my custom function(hook). Please feel free to give suggestions on the custom function that I created. (Which is not working at the moment)
The code is below:
add_action('custome_product_name', 'show_product_name');
function show_product_name(){
$product = wc_get_product(id);
echo $product->get_title();
}
Feel free to ask if there is any confusion that I caused. Cheers.
Above one is for (WC version 2.5.2) can let me know which version of WC you are using ?
Can try below action hook please:
add_action('custome_product_name', 'show_product_name');
function show_product_name(){
echo get_the_title( 'ID' );
}

Deleting a Wordpress user using a custom plugin Hook

I am new to wordpress hooks and I am trying to delete a wordpress user using a custom action the plugin Restrict Content Pro provides.
(https://docs.restrictcontentpro.com/article/2054-group-accounts-actions-filters)
What I want to achieve: When a member is removed from a group, their account should be deleted.
Unfortunately my code doesn't work. Any ideas on how to modify it be highly appreciated!
function delete_group_user() {
wp_delete_user($user_id->ID );
}
add_action( 'rcpga_remove_member', 'delete_group_user' );
You are close, your function delete_group_user() does not have $user_id defined. Luckily, it looks like the rcpga_remove_member hook provides that information. Something like this should work:
function delete_group_user($user_id) {
wp_delete_user($user_id);
}
add_action( 'rcpga_remove_member', 'delete_group_user' );
Also, note from the documentation that $user_id is an INT not an object.

Wordpress: Job Manager: How to modify Resume custom fields?

I have been trying to figure out a filter or action to modify Resume custom fields.
There is a documentation here about how to do it for resume core fields but not for custom fields.
If I use submit_resume_form_fields filter like
add_filter( 'submit_resume_form_fields', 'remove_submit_resume_form_fields' );
function remove_submit_resume_form_fields( $fields ) {
$fields only returns resume core fields but not the custom fields.
Can anyone help me?
Finally I got it worked. So instead of using submit_resume_form_fields filter, I used submit_resume_form_fields_get_resume_data and it gave me all the fields (with custom fields as well). Unfortunately I could not find a documentation of this, I had to search the code. So the code goes like
add_filter( 'submit_resume_form_fields_get_resume_data', 'remove_submit_resume_form_fields' );
function remove_submit_resume_form_fields( $fields ) {
unset( $fields['resume_fields']['custom_field_name'] );
}
I thought it might help someone!

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