When/where would want to attach other classes to the base class in a WordPress plugin?

It’s hard to answer that without some additional code to accompany it as those aren’t WordPress core functions and could be doing anything really. Based on a previous question, I’ll assume one of the plugins you’re referring to is the LiquidChurch/lqd-messages plugin and I see those methods in the main plugin file. Even if this … Read more

Store a value in global scope after init hook is fired

So after consulting on Facebook Advanced WordPress group, the advice was to create an object with setters and getters instead of using the declaring and getting variables in the global scope as shown in the code below: class MyPlugin { public $current_post_types; public function __construct() { add_filter(‘init’, array($this, ‘get_variables’)); } public function get_variables() { $this->current_post_types … Read more

Set user role, if an specific role created an user

You can use user_register action, which is invoked after registering a new user. To add or remove roles/caps, the WP_User class provides methods: add_cap(), add_role() remove_cap(), remove_role() Your code might look like this: add_action( ‘user_register’, ‘se385135_user_register’ ); function se385135_user_register( $user_id ) { $user = wp_get_current_user(); if ( !isset( $user->ID ) || $user->ID == 0 ) … Read more

Using get_terms for custom taxonomy in functions.php

You can add the action on the init itself, just increase the priority of the add_action call. The higher the priority the later the function is called. add_action(‘init’, ‘retrieve_my_terms’, 9999); But my suggestion is that you should do these kind of things as late as possible, preferably just before the first time they are used. … Read more

Preset Widgets ONLY after site is initially created

Why not output your Theme’s custom Widgets using the_widget(), inside of a if ( ! dynamic_sidebar( ‘sidebar-name’ ) ) conditional? e.g. <?php if ( !dynamic_sidebar( ‘sidebar-right’ ) ) { $widgetsidebarrightargs = array( ‘before_widget’ => ‘<div class=”widget”>’, ‘after_widget’ => ‘</div>’, ‘before_title’ => ‘<div class=”title widgettitle”>’, ‘after_title’ => ‘</div>’ ); the_widget(‘WP_Widget_Calendar’ , ‘title=” , $widgetsidebarrightargs ); the_widget( … Read more

Developing plugin, where to place action hooks etc?

You do it like in your first example // Shortcodes.php file function myplugin_shortcode( $atts ) { if ( !empty ($atts) ) { foreach ( $atts as $key => &$val ) { $val = html_entity_decode($val); } } myplugin_display_items( $atts ); } // Register shortcodes add_shortcode( ‘output-items’, ‘myplugin_shortcode’);