I have two plugin. The pseudo code is as below. I want to load CSS/JS only when that plugin is called.
Right now, all js/css from all the plugins are getting called. How do I prevent this ?
Plugin 1
class Plugin1 {
function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue')
}
function enqueue() {
wp_register_style('xxx','path');
wp_eneque_style('xxx);
}
}
Plugin 2
class Plugin2 {
function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue')
}
function enqueue() {
wp_register_style('xxx','path');
wp_eneque_style('xxx);
}
}
Related
I'm using Timber plugin, and I would like to have separate files for registering CPTs, taxonomies etc. I have a lot of those and do not want to clutter up the functions.php file. I know I can just use require_once(), but I'm not sure if it is the proper way.
class StarterSite extends Timber\Site {
public function __construct() {
...
add_action( 'init', array( $this, 'register_post_types' ) );
...
parent::__construct();
}
public function register_post_types() {
// Proper way to have this data in a different file?
}
}
when we want to do add_action inside a class we usually do like this:
class Myclassprefix_Some_Class extends Some_Class {
function __construct(){
add_action('wp_head', array($this, 'myfuncprefix_add_meta_tag'));
}
function myfuncprefix_add_meta_tag(){
echo '<meta name="description" content="This is an example meta tag" />';
}
}
Now I want to call the parent method instead.
I want to change:
add_action('wp_head', array($this, 'myfuncprefix_add_meta_tag'));
to:
add_action('wp_head', array($parent, 'some_parent_method'));
How to change $parent correctly? I know I can do like this:
add_action('wp_head', array($this, 'some_parent_method'));
function some_parent_method(){
parent::some_parent_method();
}
Is there any other way to do this without writing the function to call parent function?
You can call parent class method without writing a new function to do that. The only requirement is that parent class method is defined as public.
class ParentClass {
public function someMethod() {
print('Hi');
}
}
class ChildClass extends ParentClass {
public function __construct() {
add_action('wp_head', [ $this, 'someMethod']);
}
}
I am new to sage theme. I am trying to add custom widget in lib/setup.php file, but i get Class 'Roots\Sage\Setup\WP_Widget' not found in {path} error.
Following is my code :
class Banner_Widget extends WP_Widget {
function __construct() {
$this->WP_Widget('Banner-Widget', __('Banner Widget', 'blogerist'), $widget_ops);
add_action('save_post', array(&$this, 'flush_widget_cache'));
add_action('deleted_post', array(&$this, 'flush_widget_cache'));
add_action('switch_theme', array(&$this, 'flush_widget_cache'));
}
public function form($instance) {
//form content
}
function widget($args, $instance) {
//widget content
}
function update($new_instance, $old_instance) {
$instance = array_map('strip_tags', $new_instance);
$this->flush_widget_cache();
$alloptions = wp_cache_get('alloptions', 'options');
if (isset($alloptions['Banner-Widget'])) {
delete_option('Banner-Widget');
}
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('Banner-Widget', 'widget');
}
}
When you want to create your class by extending the WP_Widget then you should use the global namespace.
Add a \ sign before WP_Customize_Control.
class Banner_Widget extends \WP_Widget {
//....
}
When i edit a page, and then click on Add Media. In the Media Library tab, there is a loading image that never stops spinning. Then i click on the Upload file tab and try to upload an image, when i do i got this error message "An error occurred in the upload. Please try again later.".
So i started desactivating plugins to see if one of them is causins this, and indeed one custom plugin i created is causing this, and i don't know why
This plugin contains a master class, where i declared two plugins
/*
Plugin Name: blabla
Description: blabla
Version: 0.1
Author: User
*/
include_once plugin_dir_path( __FILE__ ).'/liste_emplois.php';
include_once plugin_dir_path( __FILE__ ).'/candidature.php';
class Manitou_Plugin
{
public function __construct()
{
add_action('widgets_init', function(){register_widget('Manitou_Liste_Widget');});
add_action('widgets_init', function(){register_widget('Manitou_Candidature_Widget');});
}
}
And my two plugins contain almost the same thing and same structure, here is one of them
class Manitou_Liste_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'Manitou_Liste',
__('blabla', 'wpb_widget_domain'),
array('description' => __('blabla'),)
);
}
public function widget($args, $instance) {
wp_enqueue_script('tablesorter', '/wp-content/plugins/manitou/scripts/jquery.tablesorter.min.js');
wp_enqueue_script('manitou-affichages', '/wp-content/plugins/manitou/scripts/manitou-affichages-3.0.1.js');
wp_enqueue_script('xdomainrequest', '/wp-content/plugins/manitou/scripts/jquery.xdomainrequest.min.js');
wp_enqueue_script('initmanitou', '/wp-content/plugins/manitou/scripts/initmanitou.js');
wp_enqueue_style('manitou-affichages', '/wp-content/plugins/manitou/css/manitou-affichages.css');
echo __("<div id='manitou_affichages'></div>", 'wpb_widget_domain');
}
public function form( $instance ) {
}
public function update( $new_instance, $old_instance ) {
}
}
function wpb_load_widget3() {
register_widget('Manitou_Liste_Widget');
}
add_action('widgets_init', 'wpb_load_widget3');
Do you see anithing that can cause the upload problem ?
Thanks a lot
I want override WP public function in plugin files. I want to place it on my theme functions.php file and don't want to directly edit plugin files because I want this function only applicable when using this theme only.
How can I override this public function?
class WP_Job_Manager_Post_Types {
public function application_details_email( $apply ) {
get_job_manager_template( 'job-application-email.php', array( 'apply' => $apply ) );
}
}
You could instead try the job_manager_locate_template filter:
add_filter( 'job_manager_locate_template',
function( $template, $template_name, $template_path )
{
if( 'job-application-email.php' === $template_name )
{
// modify $template;
}
return $template;
}
, 10, 3 );
to modify the template path.