Wordpress register_activation_hook not working in my class - wordpress

My folder:
my-plugin/
---inc/
------class-my-plugin.php
---my-plugin.php
In file my-plugin.php:
require_once( ABSPATH . 'wp-content/plugins/my-plugin/inc/class-my-plugin.php' );
In file class-my-plugin.php:
class MyPlugin {
public static function pluginActivate() {
add_option( '__MY_PLUGIN_ACTIVE__' , true );
}
public static function pluginDeactivate() {
update_option( '__MY_PLUGIN_ACTIVE__' , false );
}
}
register_activation_hook( __FILE__, array( 'MyPlugin', 'pluginActivate' ) );
register_deactivation_hook( __FILE__, array( 'MyPlugin', 'pluginDeactivate' ) );
But when i actvate or deactivate, it not working. Somebody can help me?

This is likely due to your use of add_option():
A safe way of adding a named option/value pair to the options database table. It does nothing if the option already exists.
Try using update_option() for both the activate and deactivate to properly affect the option value. If this doesn't work, add some error_log() writes to your actions to make sure they are firing.

Related

How do I replace the OOTB woo customer completed order email?

In a plugin I'm working on, I'm trying to replace the OOTB customer-completed-order email with a template in the plugin
Constructor:
define( 'BKF_WC_EMAIL_PATH', plugin_dir_path( __FILE__ ) );
add_filter('wc_get_template', array($this, 'bkf_customer_completed_order_template'), PHP_INT_MAX, 5);
Function:
function bkf_customer_completed_order_template($template, $template_name, $args, $template_path, $default_path) {
if( $template_name == 'emails/customer-completed-order.php' ) {
$template = trailingslashit(BKF_WC_EMAIL_PATH) . 'templates/' . $template_name;
return $template;
}
}
note the template is still pulling the default woo one
Any thoughts/ideas are welcome!
Worked it out!
Instead of the method used in my original question, here's what worked for me:
I created a new class (similar to woocommerce/includes/emails/class-wc-email-customer-completed-order.php) - for demo purposes we'll call it My_Custom_Class
I then called like so in my constructor for the parent class I was working on:
add_action('woocommerce_email_classes', array( $this, 'bk_register_email' ), PHP_INT_MAX, 1 );
And added this function:
public function bk_register_email( $emails ) {
require_once 'emails/my-custom-class.php';
$emails['WC_Email_Customer_Completed_Order'] = new My_Custom_Class();
return $emails;
}

Localize WooCommerce Extension

I am developing WC extension by its new way. Everything works like a charm instead of localization. I have generated JSON by wp i18n make-json and using in index.js like this:
import { __ } from '#wordpress/i18n';
__( 'Verify licence', 'domain' );
Unfortunatelly the strings are never been translated. Should I load the JSON myself or register the script wp_set_script_translations( 'domain-script', 'domain', plugin_dir_path( __FILE__ ) . '/languages' );
?
At server side translations work. I am really confused about the JS translation flow. Thanks for any help.
For future generations the solution is simple.
You have to call something like this:
public function extension_set_script_translations() {
$loaded = wp_set_script_translations( 'extension', 'extension', plugin_dir_path( __FILE__ ) . '/languages' );
}
private function initLocalizations() {
add_action( 'admin_enqueue_scripts', [$this, 'extension_set_script_translations'] );
}
Another important thing is name of the language JSON file.
{text-domain}-{locale}-{script-handle}.json

get_the_ID() function does not return the post id in init hook function

Please consider the code:
function mcqac_wp_enqueue_assets() {
if (is_admin()) {
wp_enqueue_script(
'mcqac-js-admin', // Handle.
PLUGIN_URL . 'build/main-admin.js',
array( 'jquery' ), // Dependencies, defined above.
filemtime( PLUGIN_PATH . 'build/main-admin.js' ), // Version: File modification time.
true // Enqueue the script in the footer.
);
$mcqacAdminData = array();
if (get_the_ID()) {
$mcqacAdminData['options'] = get_post_meta(get_the_ID(), 'mcqac_options', true);
}
wp_localize_script('mcqac-js-admin', 'mcqacAdminData', $mcqacAdminData);
}
}
add_action('init', 'mcqac_wp_enqueue_assets');
The get_the_ID() does not return anything when I am in the edit post page. Seems like init action hook is fired before the post query.
What is the solution?
Problem fixed the admin_enqueue_scripts action hook instead of init action hook. And also declare global $post to get post id from this variable.
Try the template_redirect hook instead of init.
add_action('template_redirect', 'prefix_get_page_id');

Wordpress: Load specific CSS for admin user

I want to work on my style.css file.
But It would be better if after all change and test I publish it for users.
So Is there any way to make another style.css which load just for admin user?
You need to check the logged in user role before queuing it.
So the code should be like bellow.
you need to replace the plugins_url( 'my-plugin/css/plugin.css' ) with your stylesheet's path
function register_only_for_admin_styles() {
if( current_user_can('editor') || current_user_can('administrator') ) {
wp_register_style( 'admin-style', plugins_url( 'my-plugin/css/plugin.css' ) );
wp_enqueue_style( 'admin-style' );
}
}
add_action( 'wp_enqueue_scripts', 'register_only_for_admin_styles' );
Try the code then let me know the result.
Thanks

wp_enqueue_style places css in footer instead of the header

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!

Resources