In WordPress I'd like to restrict the templates that the Editor role can create pages with. If I have 2 templates named Default Template and Advanced Template; how do I make it such that the Admin role has access to build pages off both but the Editor role can just use the "Default Template"?
Any idea how to do this please? Thank you.
You can unset the templates you want like this.
function remove_page_templates_by_role( $templates ) {
if( current_user_can('editor')) {
unset($templates['your-template.php']); // Change to your template
}
return $templates;
}
add_filter( 'theme_page_templates', 'remove_page_templates_by_role' );
Related
I am making a plugin which adds a bunch of pages to the admin view of WP. I'd really like to use Timber, specifically, the Twig templating functionality to render these pages.
While I have next to zero experience in WP and PHP in general, what attracts me to this approach is my previous familiarity with Django / Flask templates which allow me to extend a base template and specify blocks for header, content, footer. That seems trivial to do with Timber when using it to create a theme, but I can't for the life of me figure out how to make this setup work within a plugin. Sure, I can do something like this:
add_action( 'admin_menu', 'test_setup_menu' );
function test_setup_menu() {
add_menu_page(
'Tables',
'Tables',
'manage_options',
'test-tables',
'admin_page_test'
);
}
function admin_page_test() {
Timber::Render( 'test.twig');
}
But that of course will render test.twig with header and footer parts already populated from the theme. The issue specifically is that I want to be able to add information to the header or footer blocks. I know I can do this like so:
add_action('admin_head', 'add_to_head')
function add_to_head() {
...
}
But this is precisely the type of thing I'm trying to avoid, I wish to encapsulate this type of logic in a Twig template. Is there any way to make this work?
Here's an example of how to add a custom admin page for a plugin.
<?php
/**
* Plugin Name: Test Run
*/
add_action('admin_menu', 'admin_menu_cb');
function admin_menu_cb()
{
// Ref: https://developer.wordpress.org/reference/functions/add_menu_page/
add_menu_page('Test Run Admin Page', 'Test Run', 'manage_options', 'test-run', 'render_menu_page_cb', 'dashicons-schedule', 3);
}
function render_menu_page_cb()
{
Timber::$locations = __DIR__.'/views';
$data = [];
Timber::render('main.twig', $data);
}
For a more full example please see the below repo. I created it recently as a guide for anyone to use Timber in a wordpress plugin.
https://github.com/chanakasan/a-wordpress-plugin-using-timber
I am working on a plugin and that creates a custom post type and custom taxonomy for that post type. I simple created single page template that worked fine and now trying to use taxonomy template and that's never working for me. While I pasted that template in my theme folder and it works like a charm, can any one tell me how to use taxonomy template in the plugin ?
Here is my post type name: portfolio
single page template is: single-portfolio
taxonomy name is: portfolio_category
Taxonomy template is: taxonomy-portfolio_category
All those files are living in plugins main folder. Can any one point out why taxonomy works in theme folder but not in plugin folder ???.
I believe your question is answered here:
https://wordpress.stackexchange.com/questions/51022/default-taxonomy-template-in-plugin-override-in-theme, you only need to do something in reverse...
To use a taxonomy template from your plugin directory if one exists, otherwise fall back to the template in theme directory you would do something like this:
function override_tax_template($template){
// is your portfolio_category specific custom taxonomy being shown?
$taxonomy_array = array('portfolio_category');
foreach ($taxonomy_array as $taxonomy_single) {
if ( is_tax($taxonomy_single) ) {
if (file_exists(trailingslashit(BASE_PLUGIN_DIR) . 'taxonomy-'.$taxonomy_single.'.php')) {
$template = trailingslashit(BASE_PLUGIN_DIR) . 'taxonomy-'.$taxonomy_single.'.php';
} else {
$template = trailingslashit(get_stylesheet_directory()) . 'taxonomy-'.$taxonomy_single.'.php';
}
break;
}
}
return $template;
}
add_filter('template_include','override_tax_template');
I want to restrict WooCommerce products, shop page and category pages to logged in users only.
I do not want to achieve it with any plugin.
Please let me know if anybody done it before with any hook/filter/action.
Or I have to make WooCommerce template pages and add condition over there.
If I was to do this I would hook into the init action that WordPress offers.
Then do something like this in your theme's functions.php file:
function woo_check_logged_in()
{
if ( (is_product() || is_shop() ) && is_user_logged_in() )
{
}
else
{
die("You must be logged in to view this page");
}
}
add_action('init', 'woo_check_logged_in');
I haven't tested this but I believe it should get you on the right path without having to use any plugins.
I have been working on a client's WordPress Website and last day my client want to hide navigation menu and pages from author/contributor categories.
I have searched and tried some of the plugin but didn't get the exact thing. Please let me know what should i use to hide some pages from user and from navigation.
Only Admin can see all the pages and other members should see only 1 section that is allowed to visible for them.
Thank You
use this plugin to manage All roll:
http://wordpress.org/plugins/user-role-editor/
Here is the Complete function for removing each Menu and submenu from wp-admin for another user:
function remove_menus() {
global $menu, $submenu;
$restricted = array(__('Dashboard'), __('Profile'), __('Users'), __('Tools'), __('Comments'), __('Settings'), __('Plugins')); //Here you can also define the name like Pages
end($menu);
while (prev($menu)) {
$value = explode(' ', $menu[key($menu)][0]);
if (in_array($value[0] != NULL ? $value[0] : "", $restricted)) {
unset($menu[key($menu)]);
}
}
unset($menu[5]); // this is just for example
unset($submenu['edit.php'][16]); // this is just for example
}
Now You have to put a conditon for other user i.e:
$thisusername = $current_user->user_login; // this is to get the current user login
if (($thisusername == "user123")) {
add_action('admin_menu', 'remove_menus');
}
Note: You can find many plugins but all of them are not in depth like this code.Well you can try this plugin to manage your user's roles.Capability Manager Plugin
How can I set my admin theme programmatically?
Now am using public theme for anonymous users and member theme for members. Am using role theme switcher to achieve this.
Now I want my admin theme as rubik. I tried to change it from /admin/settings/admin, but it is not effecting.
Is there any way to do this? I want public theme for my site front end and rubik theme for backend.
The admin theme is stored in the variable table; you can update it in code like so:
variable_set('admin_theme', 'theme_name');
You can also assign a theme to a specific path.
To apply admin theme to path /SOMEPATH/*
function MYMODULE_custom_theme() {
if (arg(0) == 'SOMEPATH') {
return variable_get('admin_theme');
}
}
To apply admin theme to path alias /SOMEPATH/*
function MYMODULE_custom_theme() {
//drupal_get_path_alias() may interfere with Global Redirect module
$arg = explode('/', substr(drupal_get_path_alias(request_uri(), 1), strlen(base_path())));
if ($arg[0] == 'SOMEPATH') {
return variable_get('admin_theme');
}
}
To apply a custom theme to /admin/*
function MYMODULE_custom_theme() {
if (arg(0) == 'admin') {
return 'MYADMINTHEME'; //list_themes() to see available themes
}
}
Pick a function and insert it inside your module, replacing MYMODULE with a module name.