wp_enqueue_script is not Working for Editor role - wordpress

I have this code
add_action('admin_enqueue_scripts', 'my_admin_scripts_method');
function my_admin_scripts_method() {
wp_enqueue_script('admin-control2', plugin_dir_url( __FILE__ ) .'js/admin_control.js',array(), PLUGIN_VERSION, true);
}
When I'm logged in /wp-admin with an user that is administrator the js file is enqueued. However if I switch to an user that is editor role the js file is not included.
How can i fix this ? Is this the default behavior ?

If you want my 2 cents i would say that your problem reside outside that piece of code. Be 100% sure that your code is getting executed no matter what the user lvl is. For example, move it in the main plugin file outside any class scope and see if it work

Related

Wordpress functions.php file not working ...Undefined function 'wp_enqueue_style'

I don't know why I'm having this issue. I'm new to wordpress and I am following along with a 'freecodecamp' tutorial(youtube) on how to make a custom wordpress theme from scratch.
In the video, the instructor is enqueueing his style sheet using the functions.php file. Here is the exact code he is using...
Click here for code image
<?php
function followandrew_register_style() {
wp_enqueue_style('followandrew-bootstrap', get_template_directory_uri() . "/style.css", array(), '1.0', 'all');
}
add_action('wp_enqueue_scripts', 'followandrew_register_style' );
?>
Click here for error image
Visual Studios is prompting this error as soon as I write out the commands...
"Undefined function 'wp_enqueue_style', and undefined function 'add_action'.
I am including "wp_head()" inside my front-page.php to output the stylesheet.
Everything else is working fine. I am able to see my basic html setup when posting in inside the "front-page.php". My wordpress is connected correctly and I have access to the backend admin.
If I hardcode the path to the style sheet inside of front-page.php, it works fine.
So basically, any functions or commands I try to run inside the functions.php file are coming back undefined!
Nevermind, it looks like everything is working fine. I just had the wrong source path for the custom css link.
VS code is still displaying the undefined error but everything is working.
Best 5 hours Ive ever wasted :)

wp_redirect not working in custom plugin

I am trying to create custom plugin in wordpress.
We want to create a case like if user is not logged in to the system then user should be redirected login page. I tried wp_redirect and wp_safe_redirect but it is not working. here is my code.
if (isset($_SESSION['game_login'])) {
//Do Something
}else{
wp_redirect('login');
exit():
}
I am getting this warning
Cannot modify header information - headers already sent by (output started at wp-includes/class.wp-styles.php:225) in wp-includes/pluggable.php on line 1216
can someone suggest me in this scenario?
You shouldn't just start output buffers wherever unless you're specifically delaying the final output, such as modifying content on the template_redirect, using add_shortcode, or any numerous scenarios where you intend to buffer the output.
The code in your example should be encapsulated in a function and hooked to one of WordPress' many Action Hooks. Typically this kind of function is added on the plugins_loaded or init hooks.
add_action( 'init', 'redirect_anonymous_users' );
function redirect_anonymous_users(){
if( isset( $_SESSION['game_login'] ) ){
// Do Something
} else {
wp_redirect('login');
exit();
}
}
There may be several reasons causing this issue.
Try this points and hope this may have a fix
Remove blank space from files that are showing error after php ?> tag at end of file, But in your case
it is from core file, So don't modify anything in terms of code just try to remove blank space at the
ending of those files. Also remove blank space from bottom of files
wp-config.php and functions.php
If the above point does not work add this code to your wp-config.php file
ob_start();
error_reporting(0);

Wordpress - how to use authentication for other code?

I want to setup WordPress and use it's authentication for outside scripts.
For example, I'll have a script here:
http://www.domain.com/simplescript
Instead of adding custom user and password login, I thought it would be great simply to add a line at the top of the code, something like a is_user_logged_in check.
I read somewhere this can be done, the only extra being to add require_once($_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php'); at the top...
BUT... the page says the code has to be in the theme folder - which I would prefer not to have this restriction. I want to have my code anywhere on the website.
My code isn't all that complex - but does need other files + may or may not need folders and sub folders
Any ideas on how I can do what I need?
You only need to require 'wp-load.php' in your seperate PHP file. wp-load.php is in the root of your WordPress installation. You should modify the path if the script is somewhere else. Like this,
<?php
require_once 'wp-load.php';
if( is_user_logged_in() ) {
// true
} else {
// false
}
?>

Custom action on user_register hook in WordPress

I try to implement a custom function/action after a new user profile has been created in WordPress. To test the code I try to write a text in a file as proof of function execution.
Here is the code that I have inserted at the end of the active theme's functions.php file
if ( ! function_exists( 'register_for_cmsa' ) ) :
function register_for_cmsa($user_id) {
// write in an external file
// writeLogWP($msg) is a function from a file I have included in index.php
writeLogWP("A new user is: " . $user_id);
}
add_action('user_register', 'register_for_cmsa');
endif;
Therefore, I added a new user through the admin panel. As soon as I validate the standard WP add user form (wp-admin/user-new.php) I get a blank page, meaning that the above code is in trouble. But the user is added in the database (it is visible in the users' list if I comment my function). The trouble here is when executing the writeLogWP("A new user is: " . $user_id) statement inside the register_for_cmsa() function.
I tried to see if the statement works outside of the function, while always inside the functions.php file. And I noticed that it writes the message to the external file when I navigate in the WP site, BUT it publishes blank page as soon as I get into the admin dashboard section.
My code is accepted in 'site' side but it is in error in the 'admin' one.
The code is however not executed in the 'site' side because the hook is not triggered. It is triggered only if I go to the 'wp-admin/user-new.php' but ... I can' test it, as per the above reason.
I am really confused, glad to have your comments and, why not, solution.
Thanks
The index.php is not loaded when accessing the WordPress admin, hence the writeLogWP function is not being called in that case. Moving it to functions.php should solve the issue.

wp_enqueue_style not loading CSS

I am attempting to load a script using wp_enqueue_style following the advice on the Wordpres Codex, but the script doesn't seem to be included in the source. Is this the correct way to do it?
function load_scripts() {
wp_enqueue_style('bootstrap.css', get_template_directory_uri() . '/css/bootstrap.min.css');
}
add_action('wp_enqueue_scripts', 'load_scripts');
Your action and function looks fine from here. The wp_enqueue_scripts hook should work perfectly for both stylesheets and scripts.
Have you tried echoing something out in the function to see if it's actually being called at all? I.e:
function load_scripts() {
echo "Does this output to the actual page?";
wp_enqueue_style('bootstrap.css', get_template_directory_uri() . '/css/bootstrap.min.css');
}
add_action('wp_enqueue_scripts', 'load_scripts');
If it's not, you might have a problem with your placement of the code, but if you keep everything in functions.php and outside of any other scope this shouldn't be a problem.
Another thing that can cause this behaviour is if you have already registered a stylesheet or script with the same handle ("bootstrap.css" in this case). The first argument in the wp_enqueue_style() function is just a handle for internal dependency management and should be unique, try renaming it to something else and see if that fixes your problem.
Okay, first step is to ensure you are using the correct path of the CSS file.
Add the following line in the functions.php of your theme or other appropriate place.
print( get_template_directory_uri() . '/css/bootstrap.min.css' );
This should print the URL of your desired stylesheet on top of the page. Copy and paste this URL in a new tab, if it opens a stylesheet then you are good to go. Otherwise, there is a mistake in the path.
Second step is to ensure that wp_head() is being called on the page you are displaying. It can be placed in your header template or top of archives/post files etc. This function actually renders the styles and scripts on the page.
Without wp_head(), its basically useless to enque anything as you can add links and scripts manually if you know the exact paths.
Note that in admin mode there is a special hook 'admin_enqueue_scripts'. So, if there is need to load CSS in both modes, it should be done on two hooks.
function load_scripts() {
wp_enqueue_style('bootstrap.css', get_template_directory_uri() . '/css/bootstrap.min.css');
}
add_action('wp_enqueue_scripts', 'load_scripts');
add_action('admin_enqueue_scripts', 'load_scripts');
You are trying to enqueue style on wp_enqueue_scripts hook.
Try wp_print_styles.
add_action('wp_print_styles', 'load_scripts');
Also try to register style, first.
in case you don't want to use wp_head(); in your template, you could also include your styles directly:
<link rel="stylesheet" href="<?php echo get_theme_file_uri( 'style.css' ); ?>">
beware though: wp_head() includes tons of stuff (scripts, styles,...), by wordpress itself as well as most plugins, so only omit it if you know what you're doing.
Register the style first:
function register_plugin_styles() {
wp_register_style('my-plugin', plugins_url('my-plugin/css/plugin.css'));
wp_enqueue_style('my-plugin');
}
http://codex.wordpress.org/Function_Reference/wp_register_style
Do that way. Dont try to enqueue directly. And put code into functions.php of course.
Fist confirm path of the css file is correct or not if is it a correct try with below code :
function load_scripts() {
wp_enqueue_style('load-custom-css-new', get_template_directory_uri() . '/css/bootstrap.min.css');
}
add_action('wp_enqueue_scripts', 'load_scripts');
or Go with this URL.
https://developer.wordpress.org/reference/functions/wp_enqueue_style/
I had the same problem. Although I have implemented this many times before, I lost many hours to find out what went wrong with this.
Because I made my own themes, I had declared this file unit as “fuctions.php” (I lost the -n- letter).
So, besides all the things described above you should also consider, take a good look at the file name and confirm that is “functions.php” (not fuctions, not function etc).
This might be also the reason for you.
I had the same issue. I fixed it by hard refreshing the page with Ctrl + F5 to clear cache and the css loaded normally.
If you are protecting the directory with .htpasswd, the https call generated by get_stylesheet... will fail, and you might lose some time chasing that one.

Resources