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' ) );
} );
Related
I have an page (with snippet code and short code and manual php code) and it call a REST API and base Of response, I want change tag title of this page (Not in database).and it was change every call.
i try do :
in snippet code:
$GLOBALS['MyTitle'] = $response-> title;
in function:
function change_title($title) {
$variable = $GLOBALS['MyTitle'];
if (!empty($variable))
{
$title = $variable;
}
return $title;
}
add_filter('pre_get_document_title', 'change_title',500);
but it was empty every time because the function section run sooner then snippet code(Api Call).
WordPress has not any function for change title tag text? like wp_set_title('my title') to use without add_filter?
Changing title of the page dynamically by applying the filter the hook used for the filter is document_title_parts. Here is the official wordpress documentation link and below is the example. In this example i have used get method for API.
add_filter( 'document_title_parts', 'function_to_dynamic_title');
function function_to_dynamic_title( $title_parts_array ) {
$request = wp_remote_get( 'https://dummyjson.com/products/1' );
if( is_wp_error( $request ) ) {
return $data;
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
$title_parts_array['title'] = $data->title;
return $title_parts_array;
}
Screenshot for your reference
In WordPress, we have an action to change the title programmatically.
add_filter('the_title','your_callback_function');
function your_callback_function($data)
{
global $post;
// add your condition according to page
return 'Desired Title'. $post->ID;
}
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've a WordPress website and am trying to add a custom search option in the website's home page as well in the header section. Unfortunately I can't see any option to include it from the website's dashboard and decided to follow the link to create a custom one as follows:
Tutorial Link - Custom Search
and this is the code that I've tried same as the tutorial:
add_action('tickercontainer','add_search_to_footer'); //Guessinghere I've to declare the div class
function add_search_to_footer() {
get_search_form();
}
function SearchFilter($query) {
if ( !is_admin() && $query->is_search ) {
$query->set('post_type', 'post'); //OR USE 'PRODUCT'
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function storefront_search_form_modify( $html ) {
return str_replace( array('Search …','Search'), array('WooCommerce Hooks, Storefront Theme, Google AdWords...','Search Article'), $html );
}
add_filter( 'get_search_form', 'storefront_search_form_modify' );
function new_nav_menu_items($items) {
$searchicon = '<li class="search"><i class="fa fa-search" aria-hidden="true"></i></li>';
$items = $items . $searchicon;
return $items;
}
add_filter( 'wp_nav_menu_additional-resources_items', 'new_nav_menu_items' );
Though I am not sure what I am missing here and would expect some ideas to implement in an appropriate way.
Note: Right now, after including the code above, I can't see the search option in the website.
I have a bunch of pages, which are paginated using the url parameter ?page=
I am using wordpress and would like each page to have a custom made title. So far I've created a custom template an set up the following code:
$titletags = array('Title1','Title2');
if ($page==2) {$arrayno = 1}
function ChangeTitle($title) {
$title = $titletags[$arrayno];
return $title;}
add_filter( 'wpseo_title', 'ChangeTitle' );
I run this before the call to get the header. Now this code is not working, although it should change the title to Title2, when the url parameter is 2.
The code works if don't use the array inside the function, i.e. I do:
function ChangeTitle($title) {
$title = 'SomeTitle';
return $title;}
add_filter( 'wpseo_title', 'ChangeTitle' );
You have not access to $titletags array. first use global then get it. try this code:
$titletags = array('Title1', 'Title2');
function ChangeTitle( $title ) {
global $titletags, $page;
if ( $page == 2 ) {
return $titletags[1];
}
return $title;
}
add_filter( 'wpseo_title', 'ChangeTitle' );
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);