I have a bit of a unique issue that I've been struggling with all weekend and hopefully someone can help shed some light here.. I have several ACF groups that I've specified to show on both posts and pages edit page. Although I am storing values (programmatically) in the ACF fields on pages, I need to hide that group on the pages edit screen but still show it in the post edit screen.
The following code works until I try to find a condition that identifies if user is on the page edit screen rather than the post edit screen
function my_remove_meta_boxes() {
if ( get_post_type() == 'page' )
{
$out = '';
$out .= '<style>#acf-group_61699b806807f {display: none !important;} </style>';
echo $out;
}
}
add_action( 'admin_menu' , 'my_remove_meta_boxes' );
if tried get_current_screen, is_page, and get_post_type but nothing seems to work. Any help would be huge. Thanks!
You'll have to use get_current_screen() function it will give you screen object and you'll match the screen id in if statment.
you also need to use admin_head to write your custom css with style tag.
The code will be like this:
function vh_acf_remove_meta_boxes() {
$screen = get_current_screen();
$screen_id = $screen->id ? $screen->id : '';
if ( 'page' === $screen_id ) {
echo '<style>#acf-group_61699b806807f {display: none !important;}</style>';
}
}
add_action( 'admin_head', 'vh_acf_remove_meta_boxes' );
Related
When a user registers on my Wordress site, a custom post (Athlete) is automatically created, with the user being assigned as the author. The custom post essentially acts as a profile page.
On their profile page, users fill out a bunch or info, and a total_score is calculated and saved as user meta. If they do not complete all of the forms - they won't have a total_score as it is calculated on submission.
I have created a custom archive page for the posts (athletes), and used Settings > Reading > Posts Page to set it as the default posts archive.
On the post preview template (created and looped using Ele Custom Skins and Elementor) I have added an element called #total_score_circle- seen in the screenshot below.
I would like to hide #total_score_circle on the post preview layout if there is no total_score in the author meta for that post.
The below code currently hides the #total_score_circle across all post previews, and not just the ones where total_score doesn't exist in the author meta. So my query is clearly off.
Any help would be greatly appreciated.
function total_score_display(){
if (is_home()){
global $post;
$author_id=$post->post_author;
$total_score = get_the_author_meta('total_score', $author_id);
if(empty($total_score)) : ?>
<style type="text/css">
#total_score_circle {
display: none !important;
}
</style>
<?php endif;
}
}
add_action( 'wp_head', 'total_score_display', 10, 1 );
The frontend has been created with Elementor Pro, and I have used Elementor Custom Skin to create the loop that displays the search results.
Thanks to Ruvee's guidance, I managed to solve my problem through implementing a shortcode on the actual custom skin page using Elementor and the below PHP.
Essentially I created a shortcode that displayed the value with a custom CSS class .total_score_circle and then used another shortcode to run the if/else statement.
If total_score exists, return do_shortcode(), if not return a separate, irrelevant CSS class.
I'm sure it's not the elegant way to do it, but worked a treat with Elementor.
// Create shortcode to show total_score
add_shortcode('total_score_sc', 'total_score_sc');
function total_score_sc ($atts) {
$total_score = get_the_author_meta( 'total_score');
$total_score_class = '<div class="total_score_circle" >';
$total_score_class .= $total_score;
$total_score_class .= '</div>';
return $total_score_class;
}
// Create shortcode to replace above shortcode if total_score not present
add_shortcode('final_total_score_sc', 'final_total_score_sc');
function final_total_score_sc() {
global $post;
$author_id = get_post_meta( get_queried_object_id(), 'author_id' );
$total_score = get_the_author_meta( 'total_score', $author_id );
$sR = '<div class="total_score_circle_empty" >';
$sR .= '</div>';
if(empty($total_score))
return $sR;
else
return do_shortcode( '[total_score_sc]' );
}
I would like to add a logo ( get_custom_logo() ) in the Genesis navigation bar but when it's blank, there's to be text. I've achieved this in a similar fashion for the header but cannot for the primary nav. The end result DOES work but when the user goes to the "customize" part of "site identity" the change is not visible, until you hit "publish" then refresh the actual page.
I need the change to be seen in "customize" mode as the logo is being changed. Here's the code the semi-functions...
add_filter( 'wp_nav_menu_items', 'prefix_add_menu_item', 10, 2 );
/**
* Add Menu Item to end of menu
*/
function prefix_add_menu_item ( $items, $args ) {
if( $args->theme_location == 'primary' ) {
if (( has_custom_logo())) {
$thelogo = get_custom_logo();
$items .= '<li class="menu-item">' . $thelogo . '</li>';
} else {
$items .= '<li class="menu-item">Theme Name</li>';
return $items;
}
return $items;
}
}
Ideally, I'd like to apply this method throughout the site for other hooks. If anyone can point to what I'm doing wrong, that would help.
Cheers.
It won't refresh in preview unless you add a custom setting to $wp_customize for your image or you add a new PHP class for the site logo. Either way, it's not an easy fix.
I am creating my own plugin in Wordpress. With register_post_type I can view my own posts. I also created a post status with register_post_status. When I go to the summary page I can see all posts and filter on the status.
The "problem": When I go to the summary page, I can see all posts and the filters. The "All" status is always selected on default, but I want to select my custom status on default. Is this possible? Or change the URL in the menu to post_status=&post_type= ? I am talking about the admin side.
Hope someone can help me, because I can't figure it out.
I have fixed it with this code - it changes the url in the menu:
add_action( 'admin_menu', 'wpse_admin_menu', 100 );
function wpse_admin_menu()
{
global $menu, $submenu;
$parent = 'parent';
if( !isset($submenu[$parent]) )
return;
foreach( $submenu[$parent] as $k => $d ){
if( $d[0] == 'name' )
{
$submenu[$parent][$k][2] = 'edit.php?post_status=status&post_type=type';
break;
}
}
}
I am running a wholesale shop on Woocommerce. Login is required to see the prices. This is set up and working properly. Now I wish to add a logon form on every product page to only show to visitors (not logged on users).
I am using the WooCommerce Catalog Visibility plugin. This plugin offers the functionality I described above, but my theme is somehow messing it up. The plugin author says to talk to the theme developer and the theme developer says to talk to the plugin author. So now I am trying to find a workaround.
First issue: The plugin comes with a shortcode [woocommerce_logon_form] that will display a logon form. I don't want to manually add this to every existing product since I have thousands of products on my site. I am looking for a way to get it in through the code for the product page layout.
I found this code (to be added to the functions.php) to work well:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
However, it will only show text. The short code won't work when added instead of the sample text.
Second issue: The short code shows the form even when the customer is already logged in.
I am currently using this nice code that shows or hides content depending on whether the user is logged in or not:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
That shortcode works perfectly for text, but not with other shortcodes.
So the combination of these short codes: [visitor][woocommerce_logon_form][/visitor] will not show the logon form to visitors. Instead it will only show them this as text [woocommerce_logon_form].
Please help! I am sure this is probably easily fixed by someone with coding skills.
I appreciate your effort to answer to this question. Keep in mind that my understanding of code is very limited and it would be great if you can also point out in which file to add or modify code.
To make your shortcode working in php code or in php/html code you need to use a native WordPress function do_shortcode() … You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
And this will work…
To see all the different hooks you can use instead of woocommerce_single_product_summary, please see this 2 templates code to chose in case a more convenient hook:
WooCommerce single-product.php template
WooCommerce content-single-product.php template
You can also add it the same way in one of your existing short codes, this way:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
And this will work too.
See as reference this answer: Change markup in WooCommerce shortcode output
So as you can see your problem is solved on both issues
Hellow there all I tried searching everywhere but I coudn't just find it - may be because its a wiered requirement all I want to do is remove my Comment Text area in wordpress comment - I sucessfully removed URL website and name in the comment field by using following code
<?php
function remove_comment_fields($fields) {
unset($fields['url']);
unset($fields['author']);
unset($fields['email']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
?>
But not able to remove the Text area - Actually you all will thought what will I do removing all this I am using a plugin which allow you to post images in your comment and the only option I want to give to user is post images via comment. please guide me.
Two options, the comment_form_defaults filter:
add_filter( 'comment_form_defaults', 'so16856397_comment_form_defaults', 10, 1 );
function so16856397_comment_form_defaults( $defaults )
{
$defaults['comment_field'] = '';
return $defaults;
}
or the comment_form_field_comment filter:
add_filter( 'comment_form_field_comment', 'so16856397_comment_form_field_comment', 10, 1 );
function so16856397_comment_form_field_comment( $field )
{
return '';
}
Check the comment_form source code.
You can also hide the textarea comment box by taking its id set to display:none from CSS
Just simple, add this code in functions.php file
function disable_comments_everywhere( $open, $post_id ) {
return false;
}
add_filter( 'comments_open', 'disable_comments_everywhere', 10 , 2 );