implementing plugins loaded action hook in wordpress - php

My plugin code looks like below,
if (!current_user_can('administrator')){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type=\"text/css\"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options' );
}
But when I activate I get an error
Fatal error: Call to undefined function wp_get_current_user()
I could get around this by including the pluggable.php in capabilities.php. But I don't think doing a change in those files is a better way. Because wp_get_current_user() is a pluggable function it is only available after the plugins are loaded. Is there a way to use this without making changed to core files?

Instead of hiding it with CSS, I would suggest you to remove it from Menu if it's not admin, that would be a WordPress approach
add_action('admin_menu', 'dot1_remove_dahsboard_menu', 111);
function dot1_remove_dahsboard_menu(){
global $submenu, $menu;
//to check array key you want to unset in your case, print the array
echo "<pre>";
print_r($menu);
echo "</pre>";
echo "<pre>";
print_r($submenu);
echo "</pre>";
/* Unset menu array */
if( !current_user_can('manage_options') ){
unset($menu['10']);
}
/* Unset submenu array */
if( !current_user_can('manage_options') ){
unset($submenu['edit.php']['10']);
}
}

Related

php function to change css

I'm trying to add a piece of code to change css on a page. I added the code to functions.php in WordPress. However, it does not seem to work. Since I'm quite new to this there might be something quite basic wrong with the code... Any idea why it might not be working?
// This code is added to functions.php
// intro is the class name of the element I'm trying to change
add_action( 'intro', function () {
if ( is_user_logged_in() ) {
?>
<style>
display: none!important;
</style>
<?php
};
exit;
});
I got it to work by removing exit; and targeting an element:
add_action( 'wp_head', function () {
if ( is_user_logged_in() ) {
?>
<style>
.intro{
display: none!important;
}
</style>
<?php
};
});
I think what you are trying to do is change the content of the css class which I do not think you can do. Instead a solution would be to assign a css class with the propertied that you want applied to the element e.g.
<div class="<?php if ( is_user_logged_in() ) { echo 'intro';} ?>">
// Whatever you have here will get the css style applied
// if user is logged in
</div>
And inn The CSS you have the following
.intro{
display: none!important;
}
You can create multiple files for different styles e.g. another class
.outro{
display:initial;
}
And you can add it in the code as
<div class="<?php if ( is_user_logged_in() ) { echo 'intro';} else{ echo 'outro';} ?>">
// Whatever you have here will get the css style applied
// if user is logged in and if logged out then outro class will be applied
</div>

Apply CSS Rule to woocommerce Product BACKEND page

We try to apply a certain css rule to a product backend page not the frontend page.
But there are 2 types of pages one which is when you click create product and one when you click edit product.
We need to apply the css rule to both of these pages.
We achieved half of the answer by determining part of url when we click add product in the backend by the solution below:
add_action( 'init', 'bbloomer_apply_css_if_url_contains_string' );
function bbloomer_apply_css_if_url_contains_string() {
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(false !== strpos( $url, 'post_type=product' )){
echo '<style type="text/css">
#ovaem_sectionid { display: none !important }
</style>';
}
}
We hope if there is a thing like (is_Product) but for the backend can be used for both when adding a new product or when editing an existing product page to apply this css.
<body> contains .post-type-product class, use it as the first selector
function my_custom_css() {
echo '<style>
.post-type-product #ovaem_sectionid {
display: none !important;
}
</style>';
}
add_action('admin_head', 'my_custom_css' );

How to enqueue styles for one specific page template

I've got a page template which is acting as a 'Landing Page' and doesn't need specific styles from other areas of the website.
I've managed to remove the unwanted styles and add the new styles by targeting the page ID but I need it to only happen when it's a particular page template. I can't seem to get it to work when doing a check against the page template via the is_page_template() function.
In functions.php:
if ( !function_exists('scripts_and_css') ) {
function scripts_and_css() {
if(is_page(79806))
{
wp_enqueue_style('landingpage', get_stylesheet_directory_uri() . '/css/landing__page.css', '', null);
wp_enqueue_script('landingpage', get_stylesheet_directory_uri() . '/js/landing-page.js', null);
wp_dequeue_style( 'layout', get_template_directory_uri().'/css/layout.css', '', null );
}
}
}
add_action('wp_enqueue_scripts', 'scripts_and_css');
If I then change this to use the template name, it completely fails and doesn't load or remove any of the scripts or stylesheets.
My page template filename called page-landing-page.php:
<?php
/**
* Template Name: Landing Page
* The template for displaying the content for the landing pages.
?>
<?php wp_head(); ?>
// Got all my content loading in here.
<?php wp_footer(); ?>
Here's an example of what I've tried up to now in the functions.php fle:
if(is_page_template('Landing Page'))
{
// Enqueue / Dequeue scripts / styles
}
--
if(is_page_template('page-landing-page.php')) // This is the name of my page template
{
// Enqueue / Dequeue scripts / styles
}
--
if(is_page_template('landing-page.php')) // This is the name of my page template
{
// Enqueue / Dequeue scripts / styles
}
--
if(is_page_template('landing-page')) // This is the name of my page template
{
// Enqueue / Dequeue scripts / styles
}
Just cannot seem to get it to work. Any guidance would be appreciated!
This one works perfectly.
function my_enqueue_stuff() {
// "page-templates/about.php" is the path of the template file. If your template file is in Theme's root folder, then use it as "about.php".
if(is_page_template( 'page-templates/about.php' ))
{
wp_enqueue_script( 'lightgallery-js', get_template_directory_uri() . '/js/lightgallery-all.min.js');
wp_enqueue_script('raventours-picturefill', "https://cdn.jsdelivr.net/picturefill/2.3.1/picturefill.min.js", true, null);
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
For some reason, if you do not select the page template from the Template Dropdown on the edit page, is_page_template('template-name.php') doesn't seem to work.
I have found a kind-of-a-hacked solution to your problem. It seems to be working for both of the cases. Either you select the page template from the dropdown or the template gets selected by the page-slug.
if( basename( get_page_template() ) == 'page-price-watch.php' )
{
// Enqueue / Dequeue scripts / styles
}
Thanks.
"is_page_template" works by checking the post meta. If the template is automatically pulled, for example because it's called home.php, the template being used is not filled into the meta. Meta is only filled when actively selecting the template for a page in the editor.
These always work and do not rely on the meta:
function your_enqueue_styles() {
if (is_front_page()) {
//works
}
if (is_page( array('pageslug1', 'pageslug2'))) {
//works
}
global $template;
if (basename($template) === 'template-name.php') {
//works
}
}
add_action( 'wp_enqueue_scripts', 'your_enqueue_styles' );
Try something like this to display the currently used page template at the bottom of the page when viewed as an admin, makes it easier to troubleshoot:
// Page template finder
function show_template() {
if( is_super_admin() ){
global $template;
$output = '<div style="position: absolute; bottom: 0; width: 100%; text-align: center; z-index: 100; background-color: white; color: black;">';
ob_start();
print_r($template);
$output .= ob_get_clean().'</div>';
echo $output;
}
}
add_action('wp_footer', 'show_template');

Add Custom CCS to Specific Template Pages in Wordpress

I wish to centre the entry-title on certain pages in my Wordpress template using a body_class_filter. This will be based on the page template that is used.
e.g. Any page that uses page template pageshow.php should have custom CSS to centre the title.
I have read a few tutorials and comprehend what needs to be done. But my solution attempt isn't working. Here's what I tried...
I put the below at the bottom of my functions.php file...
if (is_page_template('pageshow.php'))
{ // Returns true when 'pageshow.php' is being used.
function my_class_names($classes)
{ // add 'class-name' to the $classes array
$classes[] = 'pageshowclass';
return $classes; // return the $classes array
}
// Now add pageshowclass class to the filter
add_filter('body_class', 'my_class_names');
}
else
{ // Returns false when 'about.php' is not being used.
}
And the below in my stylesheet...
/* Custom Page Styling by ID */
.pageshowclass h1.entry-title {
text-align: center;
}
But the class is not applied, nor is the css. Here is some useful doco.
Make sure your theme supports body_class. It should have the following in the body tag:
<body <?php body_class(); ?>>
If it does, check that your if condition is returning true, eg:
if (is_page_template('pageshow.php')){
echo 'ITS WORKING';
}

Check if current user is administrator in wordpress

I am developing a plugin for wordpress, I want to find if current user is administrator or not, unfortunately I could not use the current_user_can() as it gives me error, so am using the global $current_user. But I could not get inside the if part even for admin user.. How to fix this?
global $current_user;
if ($current_user->role[0]=='administrator'){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type=\"text/css\"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options' );
}
Try something like the following:
if ( current_user_can( 'manage_options' ) ) {
/* A user with admin privileges */
} else {
/* A user without admin privileges */
}
Read more about the current_user_can function here.
Get the user and check if it has the role adminstrator, like so:
function is_site_admin(){
return in_array('administrator', wp_get_current_user()->roles);
}
if (is_site_admin()) {
echo 'Woot Woot';
} else {
echo 'So sad, I have no rights';
}
This works for me:
global $current_user;
if( !empty($current_user->roles) ){
foreach ($current_user->roles as $key => $value) {
if( $value == 'administrator' ){
Do Something
}
}
}
If it's not a multi-site set-up, you can use this to detect an administrator. If it's multi-site, this will only return true for a super admin.
$user_ID = get_current_user_id();
if($user_ID && is_super_admin( $user_id )) {
Do Something
}
I know it is an old question but I would like to make this page more useful by addressing the actual issue. The actual issue here is that OP hasn't been able to use current_user_can( 'manage_options' ) in his plugin. Using the function raises the usual undefined function... PHP error. This happens because the plugin gets initialized before WP core completes loading. Fix is very simple. Loading the plugin at appropriate time is the key.
Assuming the admin plugin code resides inside a class MyPlugin, the class initialization should be hooked to init. Following is one way of doing it.
/**
* Plugin Class
*/
class MyPlugin{
public function __construct(){
/* all hooks and initialization stuff here */
/* only hook if user has appropriate permissions */
if(current_user_can('manage_options')){
add_action( 'admin_head', array($this, 'hide_post_page_options'));
}
}
function hide_post_page_options() {
// Set the display css property to none for add category and add tag functions
$hide_post_options = "
<style type=\"text/css\">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>";
print($hide_post_options);
}
}
add_action('admin_init', function(){
$myplugin = new MyPlugin();
});
This is a way of making sure that wordpress core is available to the plugin function.
You can find admin_init documentation here.
P.S. You should look into using PHP HEREDOC. It is a very simple way of writing multi-line strings. Your style block can be re-written as follows
$hide_post_options = <<<HTML
<style type="text/css">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>
HTML;
I hope it helps somebody.
Thanks.
Too late for an answer for this question, but I think it might be useful anyway if someone ends up here like me.
I needed a quick solution to this problem - check if the current user is admin or not.
From the WP codex I got a simple solution which is to use..
if(is_super_admin($user_id)) {
// do stuff for the admin user...
}
According to WP-Codex this function returns True if the currently logged in user is network (super) admin. This function returns True even if the network mode is disabled but the current user is admin.
<?php
if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber
?>
More info here: How To Check If User Is Administrator Or Editor In WordPress
use this code, I hope this solve your problem
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);
$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
//user role is admin
}

Categories