I have an question about register_activation_hook in construct.
I have read, that this should be possible, but I don't know, currently nothing will be written to the error_log (for debugging).
Look here, the author say it should work http://www.plulz.com/how-to-create-a-plugin-for-wordpress
Here's my code
<?php
abstract class LW_Capability{
const NAME = 'Capability';
public function __construct(){
register_activation_hook(
__FILE__,
array(
$this,
'activate'
)
);
register_deactivation_hook(
__FILE__,
array(
$this,
'deactivate'
)
);
}
public function activate(){
error_log('LW_Capability->activate');
}
public function deactivate(){
error_log('LW_Capability->deactivate');
}
}
class CapabilityEditRessource extends LW_Capability{
const NAME = 'EditRessource';
}
?>
What do I do wrong? If I add an "die('parent')" to the LW_Capability, it will die. The Plugin can't be activated then (WP Blocks because of output).
Anyone here who does somthing similar?
Would be happy to hear from you.
Regards,
Oli
register_activation_hook(
__FILE__,
array(
$this,
'activate'
)
);
register_deactivation_hook(
__FILE__,
array(
$this,
'deactivate'
)
);
could just be used in the main-file of the plugin ^^
I modifyed it to
register_activation_hook(
__FILE__,
array(
$MyPluginInstance,
'activate'
)
);
register_deactivation_hook(
__FILE__,
array(
$MyPluginInstance,
'deactivate'
)
);
And this functions calls manually the register / unregister-functions of the capability-class
Related
I have added an endpoint to my WP plugin (to manage extra information in my-account section in woocommerce -> my points) - my-account/moje-punkty
I did that all based on bunch of tutorials, the endpoint starts on plugin activation and is followed by a rewrite flush.
My colleague reported to me that this suddenly stops working (returning 404 error) when an user is added to the DB, although I added plenty of users while testing it and never encountered this problem.
However today I noticed that after saving Woo settings, my custom endpoint is also deleted.
Does anyone know how I can make sure that the custom endpoint stays always as long as the pluing in active?
The code reference:
function __construct()
{
register_activation_hook( __FILE__, array( $this, 'foc_points_moje_punkty_endpoints' ) );
register_deactivation_hook( __FILE__, array( $this, 'foc_points_flush_rewrite_rules' ) );
}
add_filter( 'query_vars', array ( $this, 'my_custom_query_vars'), 0 );
function my_custom_query_vars( $vars ) {
$vars[] = 'moje-punkty';
return $vars;
}
add_filter ( 'woocommerce_account_menu_items', array ( $this, 'foc_front_account_menu_points_info') );
function foc_front_account_menu_points_info( $items ) {
echo '<div class="points_dropdown_area">' . esc_html__( 'Your points', 'foc-lang' ) . ': <em>' . $punkty_meta_value . '</em></div>';
$items['moje-punkty'] = esc_html__( 'Points history', 'foc-lang' );
return $items;
}
add_action( 'woocommerce_account_moje-punkty_endpoint', array ( $this, 'foc_front_punkty_endpoint_content' ) );
function foc_front_punkty_endpoint_content() {
// my code
}
/**
* Flush rewrite rules on plugin activation.
*/
function foc_points_moje_punkty_endpoints() {
add_rewrite_endpoint( 'moje-punkty', EP_ROOT | EP_PAGES );
$this->foc_points_flush_rewrite_rules();
}
/**
* Flush rewrite rules on plugin de-activation.
*/
function foc_points_flush_rewrite_rules() {
flush_rewrite_rules();
}
Thank you!
I have a question about class written in the plugin. Let's say I have this class.
class Woo {
public static init(){
register_activation_hook( __FILE__, array( __CLASS__, 'woo_activate' ));
add_action( 'admin_menu', array(__CLASS__, 'woo_register'));
}
Woo::init();
add_action( 'plugins_loaded', array( 'Woo', 'init' ) );
My question is why put Woo::init(); there where we already have the add_action( 'plugins_loaded', array( 'Woo', 'init' ) );
Are both executed? can woo::init() be deleted and it will work fine? and vice versa?
I would like to modify a plugin called WooCommerce Smart Coupons. The plugin creates a small form on the checkout page that I would like to have at a different position.
I found this class to modify the form which has the action that I would like to remove (and replace with another action):
class WC_SC_Purchase_Credit {
/**
* Variable to hold instance of WC_SC_Purchase_Credit
* #var $instance
*/
private static $instance = null;
public function __construct() {
# some stuff
add_action( 'woocommerce_checkout_after_customer_details', array( $this, 'gift_certificate_receiver_detail_form' ) );
# some stuff
When I do this, it tells me the variable $this is undefined
remove_action( 'woocommerce_checkout_after_customer_details', array( $this, 'gift_certificate_receiver_detail_form' ) );
When I do this, it tells me the class is not found
$test123 = new WC_SC_Purchase_Credit();
remove_action( 'woocommerce_checkout_after_customer_details', array( $test123, 'gift_certificate_receiver_detail_form' ) );
Any ideas how to remove and replace this action?
$this refers to the class if called inside a class construct. You're not calling it inside the construct which is why $this doesn't work. You need to replace $this with the class name that holds the function you're trying to unhook:
<?php
remove_action( 'woocommerce_checkout_after_customer_details', array( 'WC_SC_Purchase_Credit', 'gift_certificate_receiver_detail_form' ) );
The solution to it is to get an instance of the object: WC_SC_Purchase_Credit::get_instance()
add_action('wp_loaded', 'some_function');
function some_function(){
$Purchase_Credit_Form_Object = WC_SC_Purchase_Credit::get_instance();
remove_action( 'woocommerce_checkout_after_customer_details', array( $Purchase_Credit_Form_Object, 'gift_certificate_receiver_detail_form' ) );
add_action( 'woocommerce_checkout_before_customer_details', array( $Purchase_Credit_Form_Object, 'gift_certificate_receiver_detail_form' ) );
}
I am developing a payment gateway for woocommerce.
I read many tutorial of how I can manage to do it but I can't figure out what this line of code mean:
add_action( 'woocommerce_receipt_paypal', array( $this, 'receipt_page' ) );
Especially, there is no function called woocommerce_receipt_paypal and how can I reproduce it in my plugin.
You have to replace
add_action( 'woocommerce_receipt_paypal', array( $this, 'receipt_page' ) );
with
add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ) );
where id is the one you set in __construct() function.
I have the following pieces of code that I use to include styles and java-scripts on a settings page within the WordPress admin area. I have a class with a singleton that I use to initiate my admin pages. I stripped everything except the needed pieces of code to make it more readable.
The problem that I'm having is that by using the set-up below is that the style-sheets on the settings page are placed at the bottom of the page instead of in the head of the page. I can get it in the header by using other action hooks, but that would defeat the purpose. As far as I know the set-up I used is the same setup as is described with the wp_enqueue_style command.
There is a small hint with the command "wp_enqueue_style() can now be called mid-page (in the HTML body). This will load styles in the footer.". If that is true that would mean that the 'admin_print_scripts-*' hook is called somewhere mid-page instead of at the start en doing so places the css in the footer.
Any thoughts on that. an I doing something wrong ?
Thanks for your time.
This is how the singleton class is called within the functions.php file
theme::instance( );
This is part of the class that I used to create the admin pages
class theme {
static public function instance( )
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
add_action( 'admin_menu', array( $this, 'initMenu' ), 10, 0 );
add_action( 'admin_init', array( $this, 'registerAssets' ), 10, 0 );
}
public function registerAssets( )
{
// Styles
wp_register_style( 'cen', 'style.css', array( ), '1.0' );
wp_register_style( 'cen-settings', 'settings.css', array( 'cen' ), '1.0' );
// Scripts
wp_register_script( 'cen', 'settings.js', array( 'jquery' ), '1.0', true );
}
public function initMenu( )
{
// Index page
$index =add_menu_page( 'Cen', 'Cen', 'manage_options', 'cen-index', function (){ require_once( get_template_directory( ) . '/pages/index.php' ); }, get_template_directory_uri() . '/images/icons/logo_16.png', "110.00" );
// Settings page
$settings =add_submenu_page( 'cen-index', 'Cen - Settings', 'cen' ), 'Settings', 'manage_options', 'cen-settings', function (){ require_once( get_template_directory( ) . '/pages/settings.php' ); } );
// Add action for assets on the settings page
add_action( 'admin_print_scripts-' . $settings, array( $this, 'initSettingsPage' ));
}
public function initSettingsPage( )
{
// Styles used
wp_enqueue_style( 'cen' );
wp_enqueue_style( 'cen-settings' );
// Scripts used
wp_enqueue_script( 'jquery-ui-tabs' );
wp_enqueue_script( 'cen' );
}
}
The action hook admin_print_scripts you're using is used to add inline script so it's strongly recommended to use admin_enqueue_scripts to enqueue scripts/styles in the admin.
Try it. Hope it works!