Anyone know of a way to remove the main editor from the page edit screen? And not just with css. I've added a few other meta boxes with the tinymce and they collide with the main one.
I have a class that removes other meta boxes from the edit screen, but I cant get rid of the main editor this way. I've tried to add 'divpostrich' and 'divpost' to the array in the class (but with no luck):
class removeMetas{
public function __construct(){
add_action('do_meta_boxes', array($this, 'removeMetaBoxes'), 10, 3);
}
public function removeMetaBoxes($type, $context, $post){
/**
* usages
* remove_meta_box($id, $page, $context)
* add_meta_box($id, $title, $callback, $page, $context = 'advanced', $priority = 'default')
*/
$boxes = array( 'slugdiv', 'postexcerpt', 'passworddiv', 'categorydiv',
'tagsdiv', 'trackbacksdiv', 'commentstatusdiv', 'commentsdiv',
'authordiv', 'postcustom');
foreach ($boxes as $box){
foreach (array('link', 'post', 'page') as $page){
foreach (array('normal', 'advanced', 'side') as $context){
remove_meta_box($box, $type, $context);
}
}
}
}
}
$removeMetas = new removeMetas();
I have also tried removing the 'divpostrich' with jquery. But cant figure out where to put the js for it to work. When I remove the 'postdivrich' in the browser with firebug - my remaining tinymce fields work perfect.
Any ideas?
There is built in WP support for this so you don't have to fiddle directly with the globals and ensure forwards compatibility if they ever change how features are handled. The WP Core code does pretty much the exact same logic as #user622018 answer however
function remove_editor() {
remove_post_type_support('page', 'editor');
}
add_action('admin_init', 'remove_editor');
What you are looking for is the global $_wp_post_type_features array.
Below is a quick example of how it can be used
function reset_editor()
{
global $_wp_post_type_features;
$post_type="page";
$feature = "editor";
if ( !isset($_wp_post_type_features[$post_type]) )
{
}
elseif ( isset($_wp_post_type_features[$post_type][$feature]) )
unset($_wp_post_type_features[$post_type][$feature]);
}
add_action("init","reset_editor");
Add the following code to your functions.
function remove_editor_init() {
if ( is_admin() ) {
$post_id = 0;
if(isset($_GET['post'])) $post_id = $_GET['post'];
$template_file = get_post_meta($post_id, '_wp_page_template', TRUE);
if ($template_file == 'page-home.php') {
remove_post_type_support('page', 'editor');
}
}
}
add_action( 'init', 'remove_editor_init' );
Couldn't you just disable the TinyMCE editor, leaving the HTML editor, as your meta boxes are colliding with it? :)
To disable the editor you will need to edit your wp-config.php file and add this line to the top:
define('DISALLOW_FILE_EDIT', true);
Related
I am using the Woocommerce Admin Custom Order Fields plugin which is causing issues when searching orders in the backend. When I run a slow query on the admin order search function it searches through these custom fields and adds 10secs or so to the search.
I have found the function that does it with the plugin and I'm trying to work out the best way to disable the custom fields being included in the search.
When I comment out this code the search is quick, a couple of seconds. I want to add an override or disable it somehow in my functions.php
public function add_search_fields( $search_fields ) {
foreach ( wc_admin_custom_order_fields()->get_order_fields() as $order_field ) {
if ( 'date' === $order_field->type ) {
array_push( $search_fields, $order_field->get_meta_key() . '_formatted' );
} else {
array_push( $search_fields, $order_field->get_meta_key() );
}
}
return $search_fields;
}
Can anyone give me some pointers on how to stop this executing without editing the plugin files directly?
Cheers
Nik
Don't comment all the function code, but just the active code inside the function, like:
public function add_search_fields( $search_fields ) {
/*
foreach ( wc_admin_custom_order_fields()->get_order_fields() as $order_field ) {
/* if ( 'date' === $order_field->type ) {
array_push( $search_fields, $order_field->get_meta_key() . '_formatted' );
} else {
array_push( $search_fields, $order_field->get_meta_key() );
}
}
*/
return $search_fields;
}
Now this function will not have any effect, as it's active code is commented.
Now overwriting any core plugin code is really something to avoid… There are always different ways to change things, like using available hooks and other things may be more complicated…
I'm writing a simple plugin for wordpress that changes a single word on a page or a post to make it bold.
For example: vlbs -> vlbs
It works fine for normal Wordpress pages and posts with this code:
defined('ABSPATH') or die('You can\'t enter this site');
class VLBS {
function __construct() {
}
function activate() {
flush_rewrite_rules();
}
function deactivate() {
flush_rewrite_rules();
}
function unstinstall() {
}
function new_content($content) {
return $content = str_replace('vlbs','<strong style="color:#00a500">vlbs</strong>', $content);
}
}
if(class_exists('VLBS')){
$VLBS = new VLBS();
}
add_filter('the_content', array($VLBS, 'new_content'));
//activation
register_activation_hook(__FILE__, array($VLBS, 'activate'));
//deactivation
register_deactivation_hook(__FILE__, array($VLBS, 'deactivate'));
However, it does not work on a page built with Yootheme Pro Pagebuilder. Whatever is done within the function new_content() is processed after the content has already been loaded. Thus, I cannot manipulate it before it is displayed to the user.
So the question would be: How can I get the content of a page before it is displayed? Is there an equivalent to Wordpress' 'the_content'?
Any help is really appreciated! Thank you very much in advance.
Best regards
Fabian
Yootheme: 1.22.5
Wordpress: 5.2.4
PHP: 7.3
Browser: Tested on Chrome, Firefox, Edge, Internet Explorer
In your code, do you are sure it's the good usage of add_filter content ?
In the doc, the 2nd parameter is string, not array:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
}
return $content;
}
In wordpress, the_content function display the content. There is an other function for get_the_content
Go to your page file and get the content. You can use str_replace and echo the new content after.
Example single.php :
if ( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
$content = get_the_content();
$new_content = str_replace(search, replace, $content);
echo $new_content;
endwhile;
endif;
If it is not possible for you, try to use the output buffer functions. If you need use this functions, I say it and I developpe more this part. But test the solution above before.
Oh, and it exist a special community for WP where your question will more pertinent : https://wordpress.stackexchange.com/
I'm currently using Foobox lightbox (free) plugin, and apparently the plugins' files are loaded on every page regardless of whether it's used or not. However I would like to change this, but I can't seem to find any handles to dequeue the scripts through.
It seems that the scripts are called by the code beneath, which to me at least, doesn't show any handles. I've tried using remove_action(...) to "counter" them as well as various inputs in wp_dequeue_script() to try and target the files directly - e.g. wp_dequeue_script('foobox.free.min.js')
class Foobox_Free extends Foo_Plugin_Base_v2_1 {
const JS = 'foobox.free.min.js';
const CSS = 'foobox.free.min.css';
const CSS_NOIE7 = 'foobox.noie7.min.css';
const FOOBOX_URL = 'http://fooplugins.com/plugins/foobox/?utm_source=fooboxfreeplugin&utm_medium=fooboxfreeprolink&utm_campaign=foobox_free_pro_tab';
const BECOME_AFFILIATE_URL = 'http://fooplugins.com/affiliate-program/';
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Foobox_Free ) ) {
self::$instance = new Foobox_Free();
}
return self::$instance;
}
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*/
private function __construct() {
//init FooPluginBase
$this->init( FOOBOXFREE_FILE, FOOBOXFREE_SLUG, FOOBOX_BASE_VERSION, 'FooBox FREE' );
if (is_admin()) {
add_action('admin_head', array($this, 'admin_inline_content'));
add_action('foobox-free-settings_custom_type_render', array($this, 'custom_admin_settings_render'));
new FooBox_Free_Settings();
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_GETTING_STARTED, array( $this, 'render_page_getting_started' ) );
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_SETTINGS, array( $this, 'render_page_settings' ) );
add_action( 'admin_notices', array( $this, 'admin_notice_foogallery_lightboxes' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_ignore_notice', array( $this, 'admin_notice_foogallery_lightboxes_ignore' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_update', array( $this, 'admin_notice_foogallery_lightboxes_update' ) );
add_action( 'admin_print_scripts', array( $this, 'admin_notice_foogallery_lightboxes_inline_js' ), 999 );
add_filter( 'foobox-free-has_settings_page', '__return_false' );
} else {
// Render JS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
add_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
// Render CSS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
if ( $this->is_option_checked('disable_others') ) {
add_action('wp_footer', array($this, 'disable_other_lightboxes'), 200);
}
}
}
How, if possible, do I dequeue these scripts (and styles) without editing the plugin file directly?
Edit
Below I've added the things I've tried doing to remove the scripts (all added to functions.php):
add_action( 'wp_enqueue_scripts', 'remove_foobox_scripts', 100 );
function remove_foobox_scripts() {
if ( !is_page('my-page') ) {
wp_deregister_script( 'foobox.free.min.js' );
wp_dequeue_script( 'foobox.free.min.js' );
}
}
Also tried the below, which is just a straight copy from the foobox file:
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
remove_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
Also tried the below, where the array( part is removed:
remove_action('wp_enqueue_scripts','frontend_print_scripts', 20);
remove_action('foobox-free_inline_scripts', 'inline_dynamic_js');
remove_action('wp_enqueue_scripts', 'frontend_print_styles');
The problem with the way you are trying to do this stems from the order in which things happen in Wordpress.
You are relying on the conditional tags like is_page('my-page') to determine whether or not to load the plugin. These conditional tags do not become available until Wordpress has parsed the URL for the current query, and at this point all the plugins and your theme have already been loaded. Even if you parse the URL yourself instead of using the conditional tags you cannot be sure your code will run before the plugins are loaded.
The solution is to add your code as an mu-plugin. These are loaded before normal plugins so you can use an option (option name) filter here to alter the plugins you want to be loaded.
Option filters pass an array to your function containing the values which are set for that option, so in this case you want to hook option_active_plugins.
You can find the values to use for by running print_r(get_option('active_plugins')); or look through the plugins folder of your wordpress install.
The following example is specific to your question, but you could modify it to make a more comprehensive set of rules, adding and removing multiple plugins on different pages based on many conditions.
My function checks you are not in wp-admin and then has 2 conditions. The first disables a normally active plugin on the specified pages, the second enables a normally disabled plugin on the specified pages.
<?php
add_filter( 'option_active_plugins', 'add_or_remove_plugins' );
function add_or_remove_plugins( $plugins ) {
if (strpos( $_SERVER['REQUEST_URI'], '/wp-admin/' ) !== 0) { // Disable in admin pages or admin plugin settings stop working properly
if (strpos( $_SERVER['REQUEST_URI'], '/remove-plugin-here/' ) === 0) { // Conditonal tags still unavailable so you have to parse urls yourself
$k = array_search( 'foobox-image-lightbox/foobox-free.php', $plugins ); // This will stop an active plugin from loading
if( false !== $k ){
unset( $plugins[$k] );
}
}
if (strpos( $_SERVER['REQUEST_URI'], '/add-plugin-here/' ) === 0) {
$plugins[] = 'foobox-image-lightbox/foobox-free.php'; // This will load the plugin along with all the active plugins
}
}
return $plugins;
}
?>
To install just change the values to suit, paste into a file and upload into your mu-plugins folder
EDIT
Looks like your inline js is added to wp_head during the constructor of the Foobox_Free class. You could try adding this to your functions.php:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
$foo = Foobox_Free::getInstance();
remove_action('wp_head', array($foo, 'inline_dynamic_js'));
}
or if that doesn't work then maybe this:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
remove_action('wp_head', array('Foobox_Free', 'inline_dynamic_js'));
}
The action is added inside a private function, so I don't know if either of those will actually work. Give it a shot. If not my first answer will as it stops the plugin from loading at all on the specified pages.
UPDATE
Well, I was close... Here's the code to remove the scripts, as supplied by the plugin author.
$foobox = Foobox_Free::get_instance();
remove_action('foobox-free_inline_scripts', array($foobox, 'inline_dynamic_js'));
I am developing a plugin based on woocommerce and as a part of the I had to override the default template file location of woocommerce. I mean I am looking to have a custom woocommerce template loaded from my plugin.
For doing this I read about woocommerce_locate_template in woocommerce based on this article, but I noticed that the same function has been deprecated as per this link. Now I am wondering what could be an alternative function for this.
My whole intention was to change the default woocommerce template loading location to my plugin folder. Any help in resolving this?
Thanks in advance.
woocommerce_locate_template function is deprecated in favor of wc_locate_template: you can read the code here.
However, if you are looking for the filter, it's still woocommerce_locate_template and takes three arguments:
$template that is the result of the wp core function locate_template
$template_name that is only the filename
$template_path that is the woocommerce path for templates
So you can check if the $template_name is what you want to intercept and change the path if true, like this
function intercept_wc_template($template, $template_name, $template_path) {
if ($template_name == 'that_template.php') {
$template = 'the/path/of/your/plugin/template.php';
}
return $template;
}
add_filter('woocommerce_locate_template', 'intercept_wc_template', 20, 3);
I've not tested it, so sorry for any possible syntax error :)
Hope it helps!
-- Update 1: I forgot a semicolon :P --
-- Update 2: I made a mistake! --
I had to modify the above code in order to get it to correctly match the template file I needed, which in my case was "variable.php".
$template_name needs to be the full woocommerce root path, see below:
See the amended code below:
function intercept_wc_template($template, $template_name, $template_path) {
if ($template_name == 'single-product/add-to-cart/variable.php') {
$template = 'wp-content/themes/theme-name/woocommerce/single-product/add-to-cart/variable.php';
}
return $template;
}
add_filter('woocommerce_locate_template', 'intercept_wc_template', 20, 3);
If someone struggle with this as of 2021, worth mention that the filter 'woocommerce_locate_template' does not filter all templates inside woocommerce folder. Instead, You need to filter 2 other functions:
add_filter('wc_get_template', 'entex_wc_get_template', 20, 5);
add_filter('wc_get_template_part', 'entex_wc_get_template_part', 20, 3);
For example, The root woocommerce template content-single-product.php must be filtered with wc_get_template_part.
This is working for our plugin:
function template_base(){
return untrailingslashit(plugin_dir_path( __FILE__ )) .'/templates/';
}
function entex_wc_get_template($template, $template_name, $args, $template_path, $default_path){
/* custom theme templates has priority */
if(strpos($template, '/themes/') !== FALSE) return $template;
static $cache = array();
if(isset($cache[$template_name])) return $cache[$template_name];
$plugin_template = wc_locate_template($template_name, WC()->template_path(), $this->template_base());
if($plugin_template && file_exists($plugin_template)){
$template = $plugin_template;
$cache[$template_name] = $template;
}
return $template;
}
function entex_wc_get_template_part($template, $slug, $name){
/* custom theme templates has priority */
if(strpos($template, '/themes/') !== FALSE) return $template;
$template_name = '';
if($name){
$template_name = "{$slug}-{$name}.php";
} else if($slug){
$template_name = "{$slug}.php";
}
if(!$template_name) return $template;
static $cache = array();
if(isset($cache[$template_name])) return $cache[$template_name];
$plugin_template = template_base().$template_name;
if($plugin_template && file_exists($plugin_template)){
$template = $plugin_template;
$cache[$template_name] = $template;
}
return $template;
}
This is ripped out of the PHP class and pasted here so hopefully the code is not broken.
We recommend to register the templates for cleaner performance, if you only use a few, and add something like this early in the functions:
if(!in_array($template_name, array(
'archive-product.php',
'content-product.php',
'content-product-cat.php',
'content-single-product.php',
'content-widget-product.php',
'checkout/form-checkout.php',
'checkout/thankyou.php',
'loop/loop-start.php',
'loop/loop-end.php'
))) return $template;
How could I remove classes from the body element in Wordpress?
Default:
body class="page page-id-7 page-template-default logged-in"
What I'm looking for:
body class="page-id-7"
On Post's:
body class="postid-40"
This is how you find something like this out:
Find the relevant function by looking in a theme file, in this case header.php
Look this function up in the Wordpress Codex (http://codex.wordpress.org/Function_Reference/body_class)
Is there any examples that look similar to what I want to do? Yes:
// Add specific CSS class by filter
add_filter('body_class','my_class_names');
function my_class_names($classes) {
// add 'class-name' to the $classes array
$classes[] = 'class-name';
// return the $classes array
return $classes;
}
So basically, to remove all classes, just add this to functions.php:
add_filter('body_class','my_class_names');
function my_class_names($classes) {
return array();
}
t
4. But I want to keep page id and post id - how can I do that? Because you don't know on what index in the array that you can find this information on, and searching through the array is lame, you need to first empty the array like we did above, and then add the stuff you really want. How do you get the right information, that is, page id and post id? This is step five.
(5) At the bottom of the codex page you will find a link to the source code. This is the link you'll find: http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/post-template.php If you look at the function, you'll see it uses a function which simply populates the same array that we can manipulate with aforementioned filter. By looking how they do it, you can do it too.
add_filter('body_class','my_class_names');
function my_class_names($classes) {
global $wp_query;
$arr = array();
if(is_page()) {
$page_id = $wp_query->get_queried_object_id();
$arr[] = 'page-id-' . $page_id;
}
if(is_single()) {
$post_id = $wp_query->get_queried_object_id();
$arr[] = 'postid-' . $post_id;
}
return $arr;
}
Hopefully this code works. If it does not, try to figure out what's wrong by following the steps above. I hope I helped at least a little bit :)
currently working code to remove and add a class
//remove body class
add_filter('body_class', function (array $classes) {
if (in_array('class_name', $classes)) {
unset( $classes[array_search('class_name', $classes)] );
}
return $classes;
});
// Add specific CSS class by body.
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'class-name' ) );
} );