To delete a class name 'page' from the body - wordpress

I have created a Page Templates in WordPress. The body comes in the class as the following image. I want to remove 'page' class.

You can add or remove class from body tag with the help of filter. try below script.
function remove_body_classes( $wp_classes ) {
$blacklist = array( 'page' );
$wp_classes = array_diff( $wp_classes, $blacklist );
return $wp_classes;
}
add_filter( 'body_class', 'remove_body_classes', 10, 2 );

You should add this script at the end of the Page Template that you have created .
<script type="text/javascript">
$=jQuery;
$(document).ready(function(){
$( "body" ).removeClass( "page" )
});
</script>

Filter body_class using array_search as follow works for me:
add_filter( 'body_class', function( $classes ) {
unset($classes[array_search('page', $classes)]);
return $classes;
});

add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['page'] ) ) {
unset( $classes['page'] );
}
return $classes;
});

Sorry, I wanted to do the particular for the one page.I have manually adopted the solution which is as follows.The directory path in my condition was wrong
function remove_body_classes( $wp_classes ) {
if ( is_page_template( 'inc/home-template-page.php' ) ) {
$page = array( 'page' );
$wp_classes = array_diff( $wp_classes, $page );
}
return $wp_classes;
}
add_filter( 'body_class', 'remove_body_classes', 10, 2 );

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

Hiding category posts from blog section in Wordpress

I'm trying to hide an entry from a blog section in wordpress. I have done the typical modification on function.php which is
function exclude_category( $query ) {
if ( !$query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-114' );
}
return $query;
}
add_action( 'pre_get_posts', 'exclude_category' );
And the category is still there. What I am doing wrong?. Thanks in advance
Try with only is_home condition.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', 'YOUR-CAT-ID' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Can check the condition is correct for is_home and is_main_query
function exclude_category($query) {
$query->set('cat','-114');
return $query;
}
add_filter('pre_get_posts','exclude_category');

Remove custom body class from pages with a specific category wordpress

I am trying yo Remove custom body class from pages with a specific category in WordPress.
Here is the code below I am trying to make to work. However, it does not.
function remove_body_class($wp_classes) {
if ( is_category ('places') ) :
foreach ( $wp_classes as $key=>$value ) {
if ( $value =='my_class' ) unset( $wp_classes[ $key ] );}
endig;
return $wp_classes;
} add_filter( 'body_class', 'remove_body_class');
It works when I remove class from all pages without using "if ( is_category ('places') ) :"
But I can't make it work only for specific category/posts.
Could you tell me if I do something wrong? I would highly appreciate it.
Thank you.
it will be help for you.
// Removes a class from the body_class array.
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
// 34 is your category id
if (is_category('34'))
{
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
}
// When the archive page for Category 34 is being displayed.
Define $cat_id to the category ID and change "your-class-name" to the name of the class you want removed:
if ( is_category($cat_id) ) {
add_filter( 'body_class', function( $classes ) {
if ( null !== array_search( 'your-class-name', $classes) ) {
unset( $classes[ array_search( 'your-class-name', $classes) ] );
}
return $classes;
} );
}

Add input field to every item in cart

I am trying to add a new field on my cart.php file.
I actually want to insert a URL field, so user can set a URL for each order item.
I tried to use a code from another post here but I can't get it to work.
The first and the second functions are working but when it comes to the third one, 'woocommerce_get_item_data' the $cart_item['url'] doesn't contain anything even if I add something in the field and I press Update Cart.
$cart_totals[ $cart_item_key ]['url'] from the first function is outputting the right value when the page load.
I don't know what to do now, thanks for any help.
Here is the code
Add the field
cart/cart.php
<td class="product-url">
<?php
$html = sprintf( '<div class="url"><input type="text" name="cart[%s][url]" value="%s" size="4" title="Url" class="input-text url text" /></div>', $cart_item_key, esc_attr( $values['url'] ) );
echo $html;
?>
</td>
functions.php
// get from session your URL variable and add it to item
add_filter('woocommerce_get_cart_item_from_session', 'cart_item_from_session', 99, 3);
function cart_item_from_session( $data, $values, $key ) {
$data['url'] = isset( $values['url'] ) ? $values['url'] : '';
return $data;
}
// this one does the same as woocommerce_update_cart_action() in plugins\woocommerce\woocommerce-functions.php
// but with your URL variable
// this might not be the best way but it works
add_action( 'init', 'update_cart_action', 9);
function update_cart_action() {
global $woocommerce;
if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) ) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['url'] ) ) {
$woocommerce->cart->cart_contents[ $cart_item_key ]['url'] = $cart_totals[ $cart_item_key ]['url'];
}
}
}
}
}
// this is in Order summary. It show Url variable under product name. Same place where Variations are shown.
add_filter( 'woocommerce_get_item_data', 'item_data', 10, 2 );
function item_data( $data, $cart_item ) {
if ( isset( $cart_item['url'] ) ) {
$data['url'] = array('name' => 'Url', 'value' => $cart_item['url']);
}
return $data;
}
// this adds Url as meta in Order for item
add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
function add_item_meta( $item_id, $values ) {
woocommerce_add_order_item_meta( $item_id, 'Url', $values['url'] );
}
Add a textarea field to a WooCommerce cart item
First, we just need to add the textarea field. We use the woocommerce_after_cart_item_name hook so our textarea will appear after the product name.
<?php
/**
* Add a text field to each cart item
*/
function prefix_after_cart_item_name( $cart_item, $cart_item_key ) {
$notes = isset( $cart_item['notes'] ) ? $cart_item['notes'] : '';
printf(
'<div><textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$notes
);
}
add_action( 'woocommerce_after_cart_item_name', 'prefix_after_cart_item_name', 10, 2 );
/**
* Enqueue our JS file
*/
function prefix_enqueue_scripts() {
wp_register_script( 'prefix-script', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'update-cart-item-ajax.js', array( 'jquery-blockui' ), time(), true );
wp_localize_script(
'prefix-script',
'prefix_vars',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
wp_enqueue_script( 'prefix-script' );
}
add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
´´´
At the moment, the user will be able to enter text into the field but the text won’t save. We are going to use some AJAX to save the text.
The code above not only adds the textarea to the cart item, it also enqueues a JavaScript file ready for our AJAX.
It’s assumed that you’re using the code on this page to create a new plugin. If so, you should create a new JS file with the code below and place the file in the root directory of your plugin.
However, if you’ve added the PHP above to your theme functions.php or as a snippet on your site, you’ll need to change the location of the JS file by updating line 21 of the snippet above to identify the location of the JS file.
´´´
(function($){
$(document).ready(function(){
$('.prefix-cart-notes').on('change keyup paste',function(){
$('.cart_totals').block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var cart_id = $(this).data('cart-id');
$.ajax(
{
type: 'POST',
url: prefix_vars.ajaxurl,
data: {
action: 'prefix_update_cart_notes',
security: $('#woocommerce-cart-nonce').val(),
notes: $('#cart_notes_' + cart_id).val(),
cart_id: cart_id
},
success: function( response ) {
$('.cart_totals').unblock();
}
}
)
});
});
})(jQuery);
´´´
Now, when the user types anything, the contents of the text field get sent back to the server ready to be saved as meta data to the cart item.
´´´
<?php
/**
* Update cart item notes
*/
function prefix_update_cart_notes() {
// Do a nonce check
if( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'woocommerce-cart' ) ) {
wp_send_json( array( 'nonce_fail' => 1 ) );
exit;
}
// Save the notes to the cart meta
$cart = WC()->cart->cart_contents;
$cart_id = $_POST['cart_id'];
$notes = $_POST['notes'];
$cart_item = $cart[$cart_id];
$cart_item['notes'] = $notes;
WC()->cart->cart_contents[$cart_id] = $cart_item;
WC()->cart->set_session();
wp_send_json( array( 'success' => 1 ) );
exit;
}
add_action( 'wp_ajax_prefix_update_cart_notes', 'prefix_update_cart_notes' );
function prefix_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
foreach( $item as $cart_item_key=>$cart_item ) {
if( isset( $cart_item['notes'] ) ) {
$item->add_meta_data( 'notes', $cart_item['notes'], true );
}
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'prefix_checkout_create_order_line_item', 10, 4 );
´´´
The prefix_update_cart_notes function does a security check using the WooCommerce cart nonce then saves the content of the textarea as meta data in the cart item. You can check out this article for more information about updating cart meta for items that have already been added to the cart.
Add the custom text to the order meta
Finally, we want to pass our meta data to the order so that we can use it after the customer has checked out. The prefix_checkout_create_order_line_item function takes care of that, iterating through each item and saving notes when it finds them.
https://pluginrepublic.com/how-to-add-an-input-field-to-woocommerce-cart-items/

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!

Resources