Wordpress hook after theme is deleted - php

I create theme in wordpress and now i create some theme options page in appearance.
Now if user remove theme from system i want to delete any data from database.
function register_my_setting() {
register_setting( 'settings_grup', 'array_post_redirect');
}
add_action( 'admin_init', 'register_my_setting' );
Now i want to delete options but only solution i found is hook: switch_theme and unregister_setting function. Is there any better hook to perform this ?
function un_register_my_setting() {
unregister_setting ( 'settings_grup', 'array_post_redirect');
}
add_action( 'switch_theme', 'un_register_my_setting' );

There is a very good tutorial here that shows you how to create a register_deactivation_hook for your theme. Wordpress does not have this feature for themes (like plugins) by default, but with a little tweaking your can duplicate the behavior for your theme.
For reference, it used to be done like this (pre 3.1 I believe):
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
// do your stuff
}

Related

How to Identify the code used by WooCommerce hook 'woocommerce_view_order'

Within WooCommerce I have been making some edits to templates. So far this has been straight forward.
Now I am trying to add some columns into the 'Order Details' table under 'my-account > view-order'.
I am in the template view-order.php which is a template under 'myaccount' in WooCommerce.
Instead of seeing some code in a template to edit, I am seeing the following code:
<?php do_action( 'woocommerce_view_order', $order_id ); ?>
Where is the code from this action called and can I edit it?
Thanks for all time and help.
You need to look at WooCommerce plugin includes/wc-template-hooks.php core file (line 259):
add_action( 'woocommerce_view_order', 'woocommerce_order_details_table', 10 );
As you can see, the function woocommerce_order_details_table() is hooked in. So now let's find this function that is located in includes/wc-template-functions.php core file (starting line 2584).
As you will see this hooked function call the template file order/order-details.php.
So now you can make some changes:
1). Overriding the template file order/order-details.php via your active child theme or theme as explained in this documentation.
Note: The template file order/order-details.php is also used in Order received (thankyou), so take care to target your changes using the following condition:
// For view order
if ( is_wc_endpoint_url( 'view-order' ) ) {
// Here your changes
}
// For other cases
else {
// Here keep the original code
}
2). Or/and you could also remove this hooked function to replace it by your own custom function, with something like:
remove_action( 'woocommerce_view_order', 'woocommerce_order_details_table', 10 );
add_action( 'woocommerce_view_order', 'custom_order_details_table', 10 );
function custom_order_details_table( $order_id ) {
if ( ! $order_id ) {
return;
}
// Here below add your own custom code
}
You can also call your own custom template in that custom function, that will be used exclusively in order view endpoint...
Related: WooCommerce action hooks and overriding templates
WooCommerce Documentations:
Template structure & Overriding templates via a theme
WooCommerce Conditional Tags

Redirect User based on page template used

i have wordpress blog. I use different templates for blog posts and medical cases. I installed plugin for creating custom templates per post and it do their job. But now want medical cases to be available just for logged in users.
Page that i want to manage is using my custom template:
So i search in google for function that will limit access by template and by logged in status. And writen this function:
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page_template( $template = 'templates/clinic_case.php' ) && is_single() && ! is_user_logged_in() ) {
wp_redirect( 'url/wp-login.php', 301 );
exit;
}
}
but when try to load, content is loading just fine, and should not. Any help?
My guess is that by the time Wordpress processes your code, it has already passed the template_redirect hook.

Wordpress - custom archive page for post type from plugin

I've created a pretty basic plugin which adds a custom post type with a couple taxonomies and a few custom fields. I would rather use a plugin than simply add it to the theme because I think I might be useful in the future. The problem is that I want to customize the archive page for the particular post type. I could obviously just create a new file, in this case archive-research.php but having it in the theme directory would defeat the purpose of using a plugin. Is there a method whereby I could have the custom archive file as part of the plugin itself?
Use archive_template filter, from WordPress codex: https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
<?php
function get_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'my_post_type' ) ) {
$archive_template = dirname( __FILE__ ) . '/post-type-template.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
?>

How to Disable Changing the Installed Theme in Wordpress?

I developed my own WordPress theme for my client!.
If he changes the theme from outside, he will lose my theme.
I want to disable him from changing Wordpress theme from mine theme.
How can I Disable it? is there any way by editing the Wordpress files?
You can remove theme submenu using following code. Add below code in your theme's functions.php file.
add_action( 'admin_menu', 'adjust_the_wp_menu', 999 );
function adjust_the_wp_menu()
{
remove_submenu_page( 'themes.php', 'themes.php' );
}
Restrict admin to open file
add_action( 'current_screen', 'this_screen' );
function this_screen()
{
$current_screen = get_current_screen();
if( $current_screen ->id === "themes" )
{
wp_die("You don't have access to this page.");
}
}

Why does wpmu_new_user not fire inside of a plugin but does inside functions.php?

I have a project to complete where sites within a WordPress Multisite blog will automatically add users to the corresponding sites (where the plugin is enabled)
I hope I worded that correctly.
The problem: The wpmu_new_user hook does not fire inside of a plugin but WILL inside of functions.php
This is my code:
add_action( 'wpmu_new_user', 'register_hack_action', 10, 1 );
add_action( 'wpmu_activate_user', 'register_hack_action', 10, 1);
function register_hack_action( $user_id ) {
$this_id = get_current_blog_id();
if ( !defined('ABSPATH') ) {
// do nothing
} else {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
switch_to_blog($blog['blog_id']);
if ( is_plugin_active( 'register-hack/register-hack.php' ) ) {
// add user to blog
add_user_to_blog($blog['blog_id'], $user_id, 'subscriber');
}
}
}
This works perfectly fine when you add the snippet to functions.php. But when you add it to a plugin (which goes into wp-content/plugins) and then is activated on certain sites, just will not work. if you can try it for yourself you'll see what i mean.. I don't understand why it will not work. I need it to be inside of the plugin and work.
Only some extensive debugging could answer this... But this kind of stuff is better placed inside a Must Use plugin.
Must-use plugins (a.k.a. mu-plugins) are plugins installed in a special directory inside the content folder and which are automatically enabled on all sites in the installation. Must-use plugins do not show in the default list of plugins on the Plugins page of wp-admin – although they do appear in a special Must-Use section – and cannot be disabled except by removing the plugin file from the must-use directory, which is found in wp-content/mu-plugins by default.
I don't understand why you're using that include_once, please, test it removing that.
Especulations:
as a normal plugin, it should be Network Activated and try to encapsulate the actions with:
add_action( 'plugins_loaded', function() {
add_action( 'wpmu_new_user', 'register_hack_action', 10, 1 );
add_action( 'wpmu_activate_user', 'register_hack_action', 10, 1);
});
maybe you're putting inside the main site theme?

Categories