admin_enqueue_scripts not working in specific admin page - wordpress

This is my code in functions.php file.
function my_enqueue($hook) {
if ( 'bookings' != $hook ) {
return;
}
wp_enqueue_style('datatablecss','//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
wp_enqueue_style('datatablecss2','//cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css' );
wp_enqueue_script('datatablejs','//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js',array(),'1.0',false);
wp_enqueue_script('datatablejs2','//cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js',array(),'1.0',false);}add_action( 'admin_enqueue_scripts', 'my_enqueue' );
My custom page name is bookings
and url is https://hireo.co.uk/wp-admin/admin.php?page=bookings
Any Solution for this?

Try this
if(isset($_GET['page']) && $_GET['page'] == 'options'){
wp_enqueue_style( 'akbank-bootstrap-min-css', 'https://cdn.jsdelivr.net/npm/bootstrap#3.3.7/dist/css/bootstrap.min.css');
wp_enqueue_style( 'akbank-bootstrap-theme-css', 'https://cdn.jsdelivr.net/npm/bootstrap#3.3.7/dist/css/bootstrap-theme.min.css');
wp_enqueue_script( 'akbank-bootstrap-min-js', 'https://cdn.jsdelivr.net/npm/bootstrap#3.3.7/dist/js/bootstrap.min.js');
}

if condition set is_admin()
function my_enqueue($hook) {
if ( 'bookings' != $hook ) {
return;
}
if ( is_admin() ) {
wp_enqueue_style('datatablecss','//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
wp_enqueue_style('datatablecss2','//cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css' );
wp_enqueue_script('datatablejs','//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js',array(),'1.0',false);
wp_enqueue_script('datatablejs2','//cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js',array(),'1.0',false);}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
}

Related

How do I rewrite the path in the wordpress account menu?

I have the following code in the functions.php file.
add_action( 'init', 'add_admin_tools_account_endpoint' );
function add_admin_tools_account_endpoint() {
add_rewrite_endpoint( 'wp-admin', EP_PAGES );
}
add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
if ( current_user_can('administrator') ) {
$menu_links = array_slice( $menu_links, 0,0 , true )
+ array( 'wp-admin' => __('Admin tools') )
+ array_slice( $menu_links, 0, NULL, true );
}
return $menu_links;
}
add_action( 'woocommerce_account_admin-tools_endpoint', 'admin_tools_account_endpoint_content' );
function admin_tools_account_endpoint_content() {
if ( current_user_can('administrator') ) {
echo "<h3 class='headline'>Admin Tools</h3>
<p style='text-align:center;'>Test of various functions.</p>";
}
}
My problem is that it points to: https://{PATH}/my-account/wp-admin. I need to go to https://{PATH}/wp-admin instead.
Thanks in advanced
You should remove the endpoint from the my-account page using the remove_rewrite_endpoint function. Then, in the custom_account_menu_items function, we add a direct link to the wp-admin page using the admin_url function.
add_action( 'init', 'add_admin_tools_account_endpoint' );
function add_admin_tools_account_endpoint() {
// Remove the endpoint from the my-account page
remove_rewrite_endpoint( 'wp-admin', EP_PAGES );
}
add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
if ( current_user_can('administrator') ) {
// Add the link to the wp-admin page
$menu_links['wp-admin'] = __('Admin tools');
}
return $menu_links;
}
add_action( 'woocommerce_account_menu_items', 'admin_tools_account_menu_link' );
function admin_tools_account_menu_link( $menu_links ) {
if ( current_user_can('administrator') ) {
$menu_links['wp-admin'] = 'Admin tools';
}
return $menu_links;
}
Try this..
add_action( 'init', 'add_admin_tools_account_endpoint' );
function add_admin_tools_account_endpoint() {
add_rewrite_endpoint( 'wp-admin', EP_ROOT );
}
add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
if ( current_user_can('administrator') ) {
$menu_links = array_slice( $menu_links, 0,0 , true )
+ array( 'wp-admin' => __('Admin tools') )
+ array_slice( $menu_links, 0, NULL, true );
}
return $menu_links;
}
add_action( 'template_redirect', 'admin_tools_account_endpoint_redirect' );
function admin_tools_account_endpoint_redirect() {
global $wp_query;
if ( isset( $wp_query->query_vars['wp-admin'] ) ) {
wp_redirect( site_url( '/wp-admin/' ) );
exit;
}
}
add_action( 'woocommerce_account_wp-admin_endpoint', 'admin_tools_account_endpoint_content' );
function admin_tools_account_endpoint_content() {
if ( current_user_can('administrator') ) {
echo "<h3 class='headline'>Admin Tools</h3>
<p style='text-align:center;'>Test of various functions.</p>";
}
}
In the add_admin_tools_account_endpoint() function, we have changed the EP_PAGES argument to EP_ROOT. This sets the endpoint to the root URL instead of to the account page
I have added a new function admin_tools_account_endpoint_redirect() that redirects the endpoint to site_url( '/wp-admin/' ) if the wp-admin query var is present in the URL. This is necessary because we have changed the endpoint to the root URL, so we need to manually redirect it to the correct page
We have changed the action in the add_action() call for displaying the endpoint content to woocommerce_account_wp-admin_endpoint to match the new endpoint name.
With these changes, the URL for the "Admin tools" link in the account menu should point to https://{PATH}/wp-admin, as desired

Add custom javascript to Woocommerce edit product page

how can I reference a custom js-file only to the Woocommerce edit / add product page?
Thanks in advance.
Ciao
This worked for me (to be added in themes's functions.php file):
//wholesale-prices
function add_admin_scripts( $hook ) {
global $post;
if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
if ( 'product' === $post->post_type ) {
wp_enqueue_script( 'myscript', get_stylesheet_directory_uri().'/js/wholesale-prices.js' );
}
}
}
add_action( 'admin_enqueue_scripts', 'add_admin_scripts', 10, 1 );

get_query_var don't working although it's in $wp->query_vars

I try to use get_query_var for a new parameter "forumid".
I see the parameter when I print ($wp->query_vars),
Array (
[forumid] => 23 )
But the get_query_var("forumid") return NULL
add_filter( 'query_vars', 'wpse9870_query_vars' );
function wpse9870_query_vars( $query_vars )
{
$query_vars[] = 'forumid';
return $query_vars;
}
add_action( 'parse_request', 'wpse9870_parse_request' );
function wpse9870_parse_request( &$wp )
{
if ( array_key_exists( 'forumid', $wp->query_vars ) ) {
echo get_query_var('forumid');
require 'wp-content/plugins/forum/2.php';
exit();
}
return;
}
In the file wp-content/plugins/forum/2.php I also get Null for get_query_var('forumid')
The action hook parse_request is to early.
Read here -> Action_Reference
The hook parse_request run's before parse_query where the query variables are just ben setted.
I think you should use parse_query instead of parse_request.
add_action( 'parse_query', 'wpse9870_parse_request' );
function wpse9870_parse_request( &$wp )
{
if ( array_key_exists( 'forumid', $wp->query_vars ) ) {
echo get_query_var('forumid');
require 'wp-content/plugins/forum/2.php';
exit();
}
return;
}

How to add custom stylesheet to TinyMCE in WordPress via a plugin?

I am trying to add a custom stylesheet to the TinyMCE editor in a WordPress plugin I am developing. The WP codex tells me to use the mce_css filter, but it does not seam to work.
As soon, as I use the filter, all the theme's custom stylesheets disappears from the editor, but my custom stylesheet is still not there.
See the following two screenshots, the first without the filter, the second with the filter activated:
Here is my code:
class test_plugin {
function __construct($args = array()){
if ( is_admin() ){
add_action('admin_head', array( $this, 'admin_head') );
add_action( 'admin_enqueue_scripts', array($this , 'admin_enqueue_scripts' ) );
}
}
function admin_head() {
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_css', 'plugin_mce_css' );
}
}
function admin_enqueue_scripts(){
// wp_enqueue_style('fa_icon_shortcode', plugins_url( 'css/mce-button.css' , __FILE__ ) );
}
function plugin_mce_css( $mce_css ) {
if ( ! empty( $mce_css ) )
$mce_css .= ',';
$mce_css .= plugins_url( 'editor.css', __FILE__ );
return $mce_css;
}
}
new test_plugin();
Any idea what goes wrong here?
It actually was a pretty simple solution. Since I am using OOP, I had to add the filter with
add_filter( 'mce_css', array( $this , 'plugin_mce_css' ) );
instead of
add_filter( 'mce_css', 'plugin_mce_css' );
Works like charm!

wordpress show only media user has uploaded in wp_editor

I'm creating a wordpress site where the registered user has the ability to create his own post via wp_editor() on the frontend, but just one post.
Now I want to restrict the user to be able to only see his uploaded media. I use the following script in the functions.php, which works in the backend. So if a user goes to the media section in the backend he will only see his uploaded media.
But if the user goes to "insert media" pop-up on the frontend wp_editor he can still see the uploaded media from all the users.
function restricted_media_view( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false
|| strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
if ( !current_user_can( 'level_5' ) ) {
global $current_user;
$wp_query->set( 'author', $current_user->id );
}
}
}
add_filter('parse_query', 'restricted_media_view' );
Do you have any idea hot to solve this annoyance? Thank you!
You might try this plugin: http://wordpress.org/extend/plugins/view-own-posts-media-only/
Alternatively try this:
add_action('pre_get_posts','ml_restrict_media_library');
function ml_restrict_media_library( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
return;
if( !current_user_can('manage_media_library') )
$wp_query_obj->set('author', $current_user->ID );
return;
}
Source: http://wpsnipp.com/index.php/functions-php/restricting-users-to-view-only-media-library-items-they-upload/#comment-810649773
alternatively since WordPress 3.7
add_filter( 'ajax_query_attachments_args', "user_restrict_media_library" );
function user_restrict_media_library( $query ) {
global $current_user;
$query['author'] = $current_user->ID ;
return $query;
}
I use API/Filter Reference/ajax query attachments args for WP 4.3.1 and works
add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments', 10, 1 );
function show_current_user_attachments( $query = array() ) {
$user_id = get_current_user_id();
if( $user_id ) {
$query['author'] = $user_id;
}
return $query;
}
just add on functions.php
or check this link WP Codex

Resources