How to Disable Changing the Installed Theme in Wordpress? - php

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.");
}
}

Related

How to redirect WooCommerce shop to a custom shop page

I have a website built on the Ample Business theme with the Woocommerce plugin installed and activated. We have created a custom shop page and want to redirect the Woocommerce /shop/ URL to the new page. I have read several posts that prescribe answers, but none seem to work. The page will not redirect.
What I've tried:
Added the following code to the child theme's functions.php file:
// Redirect WooCommerce Shop URL
function wpc_shop_url_redirect()
{ if( is_shop() ){
wp_redirect( home_url( '/shop2/' ) ); // Assign custom internal page here exit(); } }
add_action( 'template_redirect', 'wpc_shop_url_redirect' );
Tried a standard 301 redirect via .htaccess.
These solutions do not work and I can not simply create a new template file for archive_product.php because all Woocommerce files reside in the plugins folder as opposed to the theme folder. I do not want the file overwritten with each update.
Any suggestions would be greatly appreciated.
First, create a shop2 page. then create Page Templates then assign WordPress page template to page shop2 page.
Below is the example template file look like. you can give this file name like template-shop2.php
<?php
/* Template Name: Example Template */
get_header();
// your product display code.
get_footer();
?>
Then you can use your template_redirect and redirect to your shop2. this code will go in your active theme functions.php file.
function custom_shop_page_redirect() {
if( is_shop() ){
wp_redirect( home_url( '/shop2/' ) );
exit();
}
}
add_action( 'template_redirect', 'custom_shop_page_redirect' );
I figured it out. The issue was that I inadvertently had the parent theme activated and was editing the child theme's functions.php file. The following code added to functions.php successfully redirects the shop page:
// Redirect WooCommerce Shop URL
function wpc_shop_url_redirect() {
if( is_shop() ){
wp_redirect( home_url( '/store/' ) ); // Assign custom internal page here
exit();
}
}
add_action( 'template_redirect', 'wpc_shop_url_redirect' );

How to override woocommerce product-image template

I want to use my own custom template file (with a custom name) for product image and gallery thumbnails, so need to override the default Woocommerce product-image template by using this:
add_filter( 'wc_get_template', 'modify_product_gallery_template', 10, 5 );
function modify_product_gallery_template( $located, $template_name, $args, $template_path, $default_path ) {
if ( 'single-product/product-image.php' == $template_name ) {
$located = '... my-gallery.include.php';
}
return $located;
}
But no success yet. Is there any other way for doing this?
It's a problem of path that has to be the absolute server path using get_theme_file_path() for example.
Assuming that your custom template file is named my-gallery.include.php and if:
1) it's located in your active theme's root directory you will use:
$located = get_theme_file_path('/my-gallery.include.php');
So in your code:
add_filter( 'wc_get_template', 'modify_product_gallery_template', 10, 5 );
function modify_product_gallery_template( $located, $template_name, $args, $template_path, $default_path ) {
if ( 'single-product/product-image.php' == $template_name ) {
$located = get_theme_file_path('/my-gallery.include.php');
}
return $located;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) it's located inside a your active theme on a woocommerce subfolder, you will use:
$located = get_theme_file_path('/woocommerce/my-gallery.include.php');
3) If you want to override the template via your active theme, just copy the file from the woocommerce plugin to: woocommerce/single-product/product-image.php…
Now you can open edit the copied template and make changes without your actual code and the changes will appear once saved.
To override any woocommerce template you don't need to write any code. You can do is simply creating that file inside your active theme. For Example in your case you want to overwrite 'product-image.php' which is located inside
wp-content/plugins/woocommerce/templates/single-product/product-image.php
So for that what you can do, you can just create that template inside your path and for that you need to create the path to the template inside the theme. Like in your case create the path to the template i.e :
woocommerce -> templates -> single-product -> product-image.php
When woocommerce runs it will take the template from the theme if available else from the plugin.
Hope this make sense.
You can override woocommerce product-image template
project/wp-content/plugins/woocommerce/templates/single-product/product-image.php
Create below folder structure and copy above page and do modification as per your requirement
project/wp-content/themes/yourtheme/woocommerce/templates/single-product/product-image.php

WordPress - remove menu item added by plugin for non-admins

How can I remove the following menu item added by a plugin?
admin.php?page=stock-manager
WP Admin Menu Editor Plugin is one of plugins to let you have full control over your wp-admin menu.
This was the answer to my question...
function wpse28782_remove_menu_items() {
if( !current_user_can( 'administrator' ) ):
remove_submenu_page( 'admin.php', 'stock-manager' );
add it to your theme's functions.php

Wordpress hook after theme is deleted

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
}

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