Can't use WordPress Plugin Hooks - wordpress

I'm using the WordPress plugin Ultimate member which has a hook called um_user_register which should happen after a user fills out a registration form. According to the documentation (https://docs.ultimatemember.com/article/1308-umuserregister) I should be able to use that hook and do my thing. Unfortunately nothing happens.
The code I have added to my theme's functions.php file:
//Ultimate Member - Extra stuff after user registers
add_action( 'um_user_register', 'my_user_register', 10, 2 );
function my_user_register( $user_id, $args ) {
add_user_meta( $user_id, 'user_utlimate_member_signup', 'Yes');
}
Am I putting my add_action or function in the wrong place or is there something I'm overlooking?

Related

Remove license message in Wordpress

I'm trying to disable the license notification for a plugin a bought a few months ago. Now my license for updates is not longer valid but I don't want to receive any updates anymore because I've changed a lot in the plugin. This is the message:
I've tried to remove it this way:
add_filter( 'site_transient_update_plugins', 'filter_plugin_updates' );
function filter_plugin_updates( $value ) {
unset( $value->response['cred-frontend-editor/plugin.php'] );
return $value;
}
But when I refresh the page the notification is still there. How can I remove it?
This depends on the plugin. But the steps I would take.
Search to plugin codebase for variations of add_action( 'admin_notices', 'callback_notice function')
Then try to use remove_action to de-hook that notice.
Keep in mind remove_action has to run after the add_action, So if that add_action is done inside an add_action('admin_init', ...,10) (prio 10) you should run the remove action in an add_action('admin_init', ...,11)
Yes this part is confusing.
Good luck
Edit
Given the code you provided the remove should be:
add_action( 'init', function() {
remove_action( 'admin_notices', array( WP_Installer::instance(), 'setup_plugins_page_notices' ) );
remove_action( 'admin_notices', array( WP_Installer::instance(), 'setup_plugins_renew_warnings' ), 10 );
remove_action( 'admin_notices', array( WP_Installer::instance(), 'queue_plugins_renew_warnings' ), 20 );
}, 100);
The add_actions are run in the init function. the init function is hooked in the , well the init hook, at default priority (10)
So we run the removes after that in the init hook at prio 11.
Probably you don't need to remove the setup_plugins_page_notices hook.

Adding author info outside the loop in Genesis

I'm posting this question as I'm having a little trouble adding the author info to my post hero on my website.
I'm using the Genesis framework with Wordpress, so what I did is removing the post info from the post and adding it back in the post hero. That all works, except that the author name is not being shown anymore as it's not yet fetched in the post loop.
// Remove entry title
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
// Remove post info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
// Add page title
add_action( 'hero-info', 'genesis_do_post_title' );
// Add page info
add_action( 'hero-info', 'genesis_post_info', 12 );
To be able to add the post author info back in the post hero, I've looked up stackoverflow and found a link where the OP was able to fix it by creating a shortcode for it and running it in the hero-info
function author_shortcode() {
global $post;
$author_id=$post->post_author;
the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');
This shortcode [author] is then added into
add_filter( 'genesis_post_info', 'custom_post_info' );
function custom_post_info( $post_info ) {
if ( is_archive() || is_home() ) {
$post_info = __( 'Article by [author] [post_author_posts_link] on [post_date] - [post_comments zero="Leave a Comment" one="1 Comment" more="% Comments" hide_if_off="disabled"]', 'tcguy' );
return $post_info;
}
}
This is the result now: http://imgur.com/a/6lX5J
It is shown in the wrong place for some reason. Anybody knows how this can be?
The site can be found here: http://websforlocals.com/business/
Hope I gave enough info, and that someone with the same problem can be helped out.
It is issue in your ShortCode registering php code.
When adding short-code we should not ECHO anything as this way it will not be echoed at the place we want to but at the top of the post content.
So always return the output in short code function, and then echo the short-code function.
Now WordPress have a convention for functions which echo the result and which returns the result, i.e. the_author_meta vs get_the_author_meta (first one which you are using will display/echo the result, however get_ functions will return the values).
We need to use get_the_author_meta instead of the_author_meta in your shortcode registering block and it will solve your displaying location issue.
function author_shortcode() {
global $post;
$author_id=$post->post_author;
return get_the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');

wordpress: detecting rewrite rules flush

I have my own plugin that, on activation, adds some rewrite rules which map url's to single page applications.
Now, I lose the rules once in a while, after some other plugin updates or maybe another process that triggers flush_rewrite_rules in Wordpress.
The problem is, I do not know when, how or why it happens. And by whom.
So I have searched globally in my WP files and come up with the following actions that trigger a refresh
add_action( 'woocommerce_settings_saved', array( $this, 'load' ) );
add_action( 'woocommerce_attribute_added', array( $this, 'load' ) );
add_action( 'woocommerce_attribute_updated', array( $this, 'load' ) );
add_action( 'woocommerce_attribute_deleted', array( $this, 'load' ) );
add_action( 'wp_install', array( $this, 'load' ) );
add_action( 'after_db_upgrade', array( $this, 'load' ) );
add_action( 'after_switch_theme', array( $this, 'load' ) );
When one of those actions are triggered, I reload my rules.
That works, when one of those triggers.
So probably, once in a while another one triggers that I can't find.
Or maybe the flush happens after the action triggers, rendering my load useless.
So, is there a better way to hook into a flush so that I can add my own rules there automatically?
Now we figure it out a couple of days later when someone complains that the app is not working and I have to load the rules again.
Any suggestions are appreciated.
Cheers
I can see a couple of options; fixing the symptoms, or tracking down the cause.
Fixing the symptoms
At the end of the WP_Rewrite::rewrite_rules() function is a filter rewrite_rules_array:
$this->rules = apply_filters( 'rewrite_rules_array', $this->rules );
You could hook into that and make your rules are there (adding them if they don't exist). Hook it with a high number for the $priority argument, in case the rules are being removed using the same hook.
Tracking down the cause
Alternatively, you could change the flush_rewrite_rules function itself (assuming that's the entry point for the rogue changes that are deleting your rules), and have it email you a stack trace whenever it's called. It might help you track down the cause. Something like (completely untested):
function flush_rewrite_rules( $hard = true ) {
global $wp_rewrite;
$e = new Exception();
wp_mail ( 'you#example.com', 'flush_rewrite_rules called', $e->getTraceAsString() );
$wp_rewrite->flush_rules( $hard );
}
Obviously that change is only temporary, until you have a lead on the problem - I'd never advocate changing core code permanently.
If that doesn't work, you may need to put it deeper into $wp_rewrite, like the generate_rewrite_rules function.

WordPress plugins wp_enque_script()

so i'm developing my first WordPress plugin and i am having some difficulties...
I am doing it Object Oriented...
In the bottom, when 'plugins_loaded', i Create a new instance of myClass. It also enques a javascript, everytime any page is loaded. This script registration works, because i get a console.log every page load. It then registers an action on 'publish_post' that is fired when an admin publishes(saves) a new post and invokes my publish_post() method.
The method is called, when a post is published; i know it because if i uncomment it's two first lines, the sctipt dies with my var_dump.
My problem is that wp_enque_script() is not working in this method. For some reason my script isn't called...
Here's the code:
<?php
class myClass{
function __construct(){
// hooks & filters..
add_action( 'publish_post', array($this, 'publish_post'));
wp_enqueue_script(
'plugin', //$handle
plugins_url('/js/plugin.js', __FILE__)//$src
);
}
function publish_post(){
//global $wp_query;
//die(var_dump($wp_query));
wp_enqueue_script(
'publish', //$handle
plugins_url('/js/publish.js', __FILE__)//$src
);
}
}
/* Initialise outselves */
add_action( 'plugins_loaded', create_function( '', 'global $myObject; $myObject = new myClass;' ));
?>
Anyone has any idea why this is happening? thanx
Just had the same problem. You have to add it to a hook, for example the init (I tried with admin_head hook but that didn't work so I picked init because I saw it in another plugin. And it seem to work fine for me)
In you construct add:
add_action('init', array($this, 'loadMyScripts'));
and in the function called by the action:
public function loadMyScripts()
{
wp_enqueue_script(
'publish', //$handle
plugins_url('/js/publish.js', __FILE__)//$src
);
}

Do I have to add_filter() before apply_filters() in Wordpress?

I'm trying to understand Wordpress plugin like:
apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
I'm looking for all codes in Wordpress, I can't find:
add_filter( 'gettext', ....);
Why there is no add_filter for this plugin? Or I missed something? Same thing like:
do_action('wp_loaded');
I can't find:
add_action('wp_loaded', ....);
apply_filters is like, 'if there are any filters with this name, run the attached callbacks with these parameters'. So if there is no add_filter for that name, it means that there is no filter that's going to be run with the apply_filters call at the moment.
The same goes with do_action and add_action.
I am a beginner in PHP - WordPress stack as well, but this is from my understanding.
The plugins call apply_filters without having any add_filter in their codes is to allow the website users to add custom logic to their plugins. We - the users, can add our own function and use add_filter to register our functions.
For example, this piece of code is from the plugin. Normally, it shows all products but it provides us a way to not show a specific product.
// Plugin's
if (apply_filters( 'plugin_show_products', true, $product->get_id() ) ) {
$this->show_products();
}
So, if we - the users, want to customize a bit. We can add our own function as following (maybe in functions.php)
// Our custom changes
function my_own_changes($boolean, $product_id) {
if ( $product_id === 5 ) return false;
return true;
}
add_filter( 'plugin_show_products', 'my_own_changes', 10, 2 );
This translates to: The plugin will behave normally but for my own site, it will not show the product with ID of 5!
I have come across this type of code in a plugin or theme where the apply_filter is used without necessarily having an existing filter or add_filter
In this case, where the apply_filters is used without a filter you will have to call the function again where you want to run it. For example, in the header of a theme.
The following is an example of apply filters used in a function that is again called in the header.php
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo apply_filters( 'header_apply_filter_test', $output );//used here
}
}
Now in the header.php file, you would have to call this function since it is not hooked anywhere. So, in this case, to display the output in the header you would call the function like this :
<?php header_apply_filter_test(); ?>
You could as well write this code with a hook and it would do the same thing i.e display the output in the header.
add_filter('wp_head', 'header_apply_filter_test');
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo $output;
}
}
For this second option, you would still have the capability of using apply_filters anywhere else to call the callback function header_apply_filter_test() since the filter now exists.
So the bottom line in my view is a use case since either approach works!

Resources