How to hide all plugins installed in a WP site? - wordpress

I want to hide all the plugins installed in my site. How I can achieve this. I tried with the following but it does not seem to work.
function hide_plugins($plugins)
{
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = get_plugins();
foreach($plugins as $plugin){
if (in_array($plugin, array_keys($plugins))) {
unset($plugins[$plugin]);
}
}
return $plugins;
}
add_filter('all_plugins', 'hide_plugins');
Source : http://www.wpstuffs.com/hide-installed-plugins-from-dashboard-users-can-not-deactivate-the-plugin/

You don't need anything fancy.
add_filter('all_plugins', '__return_empty_array');
That does the trick for me.

Related

Allow contributors to upload images but not delete them

I enabled image uploads for contributors on my Wordpress site via the following code:
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
It works great, but I also need to disallow them to delete the images they uploaded. Is there an easy way to do it? I know there's User Role Editor plugin, but I don't want to install it just for this.
You can use delete_attachment hook or pre_delete_attachment filter for that:
add_action('delete_attachment', 'wp66410923_prevent_delete_attachment', 11, 1);
function wp66410923_prevent_delete_attachment($postID)
{
if (current_user_can('contributor')) {
echo __('You can not delete attachments.');
die;
}
}
add_filter( 'pre_delete_attachment', 'wp66410923_filter_attachment_deletion', 10, 3 );
function wp66410923_filter_attachment_deletion( $delete, $post, $force_delete ){
if (current_user_can('contributor')) {
return false;
}
return true;
}

Is there a way to just show stock on a single product when a user is logged in

I'm trying to just show stock on a single product when a user is logged in. I've tried manipulating two different scripts I've found, but it's not working. Please help!
function show_stock() {
global $product;
if ( $product->get_stock_quantity() ) { // if manage stock is enabled
if ( number_format($product->get_stock_quantity(),0,'','') < 3 ) { // if stock is low
echo '<div class="remaining">Only ' . number_format($product->get_stock_quantity(),0,'','') . ' left in stock!</div>';
} else {
echo '<div class="remaining">' . number_format($product->get_stock_quantity(),0,'','') . ' left in stock</div>';
}
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
add_action( 'init', 'hide_stock_not_logged_in' );
function hide_stock_logged_in() {
if ( !is_user_logged_in() ) {
remove_action('woocommerce_after_shop_loop_item','show_stock', 10);
}
}
You may,
1- Use wp_footer hook, which is called every page load
2- Check if user logged in inside this hook
3- If no, just make "display:none" for css attributes (class or id) of stock item.
Example:
function hide_stock_if_user_not_logged_in()
{
if ( !is_user_logged_in() )
{
echo "<style>p.stock.in-stock { display: none }</style>";
}
}
add_action( 'wp_footer', 'hide_stock_if_user_not_logged_in' );
You may need to change the "style" part. Tested and works fine. I hope this will help you.
#MrEbabi 's solution is perfect but as an alternative you could also just use css like so:
.logged_in .remaining{
display:none;
}

WordPress remove legacy CSS and include new CSS

Trying to remove old CSS and include new CSS, however when I inspect my code I still get the old CSS and not the new, have I written the code correctly?
My code
function se_remove_styles() {
if ( is_page_template( 'template/page-main.php' ) ) {
wp_dequeue_style( 'styles', get_template_directory_uri().'/assets/css/vendor.min.css',array(),'1.0.0');
wp_enqueue_style('styles',get_template_directory_uri().'/assets/css/vendornew.min.css',array(),'1.0.0');
}
}
add_action( 'wp_print_styles', 'se_remove_styles', 99 );
Probably something related to cache.
One way to update CSS in Wordpress without having to refresh the cache through the browser is to update its version.
As you see, the versions are 1.0.0 in both CSS you are calling. Change them to 1.0.1 and test it again. Like this:
function se_remove_styles() {
if ( is_page_template( 'template/page-main.php' ) ) {
wp_dequeue_style( 'styles', get_template_directory_uri().'/assets/css/vendor.min.css',array(),'1.0.1');
wp_enqueue_style('styles',get_template_directory_uri().'/assets/css/vendornew.min.css',array(),'1.0.1');
}
}

TinyMCE - Custom plugin makes visual editor blank in wordpress

I am trying to add TinyMCE custom plugin in wordpress that change the direction of selected text by using <bdo>. I register the plugin in wordpress:
/*Register Custom TinyMCE plugin*/
add_filter('mce_external_plugins', 'my_tinymce_plugins');
function my_tinymce_plugins() {
$plugins_array = array(
'tiny' => 'tiny.js' //Plugin directory is same as theme's funtion.php
);
return $plugins_array;
}
But it hide visual editor completely & also make text editor, un-editable. What is wrong?
I think you're overwriting all the other plugins, rather than just adding yours. Try
function my_tinymce_plugins($plugin_array) {
$plugin_array['tiny'] = 'tiny.js';
return $plugin_array;
}
instead. You may need to prepend get_stylesheet_directory_uri() to tiny.js to ensure the URL is correct.
Edit
Further to your comment, here's some code I used a few years ago to add a button. I can't say for certain best practices haven't changed, but it worked for me:
add_action('init', 'immo_add_column_button');
function immo_add_column_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
return;
}
if ( get_user_option('rich_editing') == 'true') {
add_filter('mce_external_plugins', 'immo_add_column_tinymce_plugin');
add_filter('mce_buttons', 'immo_register_addcolumn_button');
}
}
function immo_register_addcolumn_button($buttons) {
array_push($buttons, "|", "addcol");
return $buttons;
}
function immo_add_column_tinymce_plugin($plugin_array) {
$plugin_array['addcol'] = get_bloginfo('stylesheet_directory').'/js/immo_column_button.js';
return $plugin_array;
}
add_filter( 'tiny_mce_version', 'immo_refresh_mce');
function immo_refresh_mce($ver) {
// Force refresh of TinyMCE cache by updating the version number
$ver += 3;
return $ver;
}

Why won't WordPress load thickbox and media-upload scripts?

I'm working on a plugin that uses thickbox and media-upload to set some images. Neither will load using this code:
function add_my_files() {
echo 'happy happy happy';
wp_register_style( 'adminstyles', plugins_url('/css/slider.css', __FILE__));
wp_enqueue_style( 'adminstyles' );
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('hdjs',plugins_url('/js/slider.js', __FILE__),array('media-upload','thickbox'),'',true);
wp_enqueue_script('hdjs');
}
add_action( 'admin_init', 'add_my_files' );
my css and js files load but not thickbox and media-upload.
Thanks
The correct hook to include your asset files in WP is admin_enqueue_scripts:
NOTE: I recommend you too use get_current_screen (see is_my_admin_screen() definition below) to just include your js/css files when you actually needed.
add_action('admin_enqueue_scripts', 'add_my_files');
function add_my_files()
{
/*
* a good WP citizen only loads
* their javascript/css where it is needed
*/
if ( ! is_my_admin_screen()) // defined below
return;
wp_register_style('adminstyles', plugins_url('/css/slider.css', __FILE__));
wp_enqueue_style('adminstyles');
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('hdjs', plugins_url('/js/slider.js', __FILE__), array('media-upload', 'thickbox'), '', true);
wp_enqueue_script('hdjs');
}
function is_my_admin_screen()
{
$screen = get_current_screen();
if (is_object($screen) && $screen->id == 'my_plugin_page_id') // var_dump($screen->id); find your own id
return true;
else
return false;
}
ref: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
ref: http://codex.wordpress.org/Function_Reference/get_current_screen
Besides hopefully you are using a class to wrap all your plugin or you will have worse problems than this.
Please feedback. I am very interested in this issue because WP plugins puts food and beers on my table.

Resources