Make php do_action conditional upon class field - php

Sorry a noob question. I wish to make this:
<?php do_action('woocommerce_product_thumbnails'); ?>
Conditional to a custom field called 360 with the value yes.
<?php // do_action('woocommerce_product_thumbnails'); // ?>
For the simple reason that my plugin uses 36 images to create a rotating display.
When I dont use the plugin, I want to use the thumbnails. Many thanks Asa.
I have tried
<?php
if (get_post_meta($post->ID, '360', false)) {
do_action('woocommerce_product_thumbnails');
} else {
do_action('woocommerce_product_thumbnails');
}
?>
But I still get to see the thumbnails when 360 is returned as false?
I have tried
<?php
if (get_post_meta($post->ID, '360', true)) {
// do_action('woocommerce_product_thumbnails');
} else {
do_action('woocommerce_product_thumbnails');
}
?>
Works! Many thanks.

I think this is what you mean - if there is a value for the custom meta value of 360 (ie you'd created a custom metabox with a checkbox with id 360 in it) for that page/post then the product thumbnails are used. If not then you put in your rotating display.
<?php
if (get_post_meta($post->ID, '360', true)) {
do_action('woocommerce_product_thumbnails');
} else {
// your rotating display code.
}
?>

Related

Changing logos of site based on class - PHP Wordpress

I have this code in my header.php to change the website's logo based on the page's slug:
$logo_img = 'default';
if (is_page('brookside')) {
$logo_img = 'brookside';
} elseif (is_page('hilltop')) {
$logo_img = 'hilltop';
} elseif (is_page('reserve')) {
$logo_img = 'reserve';
}
Which works great. However, the pages a need to change the logo are query string URL, so they don't have slugs. I was able to add a unique class to each page. My question is:
How do I translate that code to "if a page has "x" class? So, what function should I use instead of is_page?
Any help would be very appreciated!
Thanks!
You can use:
if($_GET['ct_additional_features'] == 'brookside') {
$logo_img = 'brookside';
}
You could use get_class that outputs the Class Name and there solve your if with what are you saying

Displaying admin notice dynamically

I would like show the admin notice while post text is being edited (before Save post). I suppose admin_notice hook wont work in this (it doesn't for me). Any idea how to show some error message to alert the user that Post content is not going to be accepted ?
I prefer inline display like admin notice instead of popups that could be blocked.
Following doesn't work
function secure_oembed_filter($html, $url, $attr, $post_ID) {
if (strlen($html) > 0 && strpos($html, "http://")) {
//wp_die("Unsecure link in embeds", "Unsecured post");
} else {
add_action('admin_notices', array($this, 'show_error'));
}
return $html;
}
private function show_error () {
echo '<div class="error"><p>This is an error</p></div>';
}
In that case wp_remote_get() will work fine.
Create a notice.php file and uploaded it on my server.
Then I have added these code into my plugin and it works for me.
<?php
$remote_data = wp_remote_get( 'http://example.com/notice.php' );
$remote_body = wp_remote_retrieve_datat( $remote_data );
?>
<div class="notice">
<?php echo $remote_body ?>
</div>
Hope that helps.

(WordPress) How to use if OR if post?

I want to show some HTML code for specific posts, so I put this on HEADER templete:
<?php if(is_single(24) || is_single(34)) { ?>
MY HTML CODES
<?php } ?>
I tried those too:
<?php if(is_single(24) && is_single(34)) { ?>
<?php if((is_single(24)) && (is_single(34))) { ?>
<?php if((is_single(24)) || (is_single(34))) { ?>
And its not working. If I put this code for single post, like this:
<?php if(is_single(24) { ?>
Its working well.. but I need to do that for many posts.
You could do if (is_single() && in_array($post->ID, array(24, 34)) {}
Depending on context, you may need to make $post global.
I'd probably add some metadata to the posts though and check for that instead... Less messy.
Try using in_array
<?php if(in_array(POST_ID, array(POST_IDS))) { ?>
MY HTML CODES
<?php } ?>

WP WordPress theme issue

I am building a free WP theme but know next to nothing about PHP. I don't know what to put around this code and would greatly appreciate if somebody could put the correct PHP tags around it, as telling me what to put is a bit like speaking Latin to me. I tried putting <?php at the beginning and ?> at the end but it didn't work:
if ( has_post_format( 'aside' ) {
// aside format post content here
} else if (has_post_format('gallery')) {
// stuff to display the gallery format post here
} else if (has_post_format('link')) {
// link format post content here
} else if (has_post_format('video')) {
// video format post content here
} else if (has_post_format('audio')) {
// audio format post content here
} else if (has_post_format('status')) {
// status format post content here
} else if (has_post_format('chat')) {
// chat format post content here
} else if (has_post_format('quote')) {
// quote format post content here
} else if (has_post_format('image')) {
// image format post content here
}else {
// code to display the normal format post here
}
Put <?php in front of every line that does not hold any code (thus starts with if or }),
and end those lines with ?>.
Also, this first line is wrongs.
if ( has_post_format( 'aside' ) {
should be
if ( has_post_format( 'aside' ) ) {

Custom add_action('save_post') causes html markup to disappear!

I've added a custom "save_post" action to my theme (code is below). However, when I place images or video code in the post, its stripped away. The only way I can get it to stay is to comment out the add_action line. What do I need to do in order to keep all the post info intact?
add_action('save_post', 'custom_add_save');
function custom_add_save($postID){
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $postID;
}
else
{
// called after a post or page is saved
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}
if ($_POST['my_customHeader'])
{
update_custom_meta($postID, $_POST['my_customHeader'], 'my_customHeader');
}
else
{
update_custom_meta($postID, '', 'my_customHeader');
}
if ($_POST['my_customTitle'])
{
update_custom_meta($postID, $_POST['my_customTitle'], 'my_customTitle');
}
else
{
update_custom_meta($postID, '', 'my_customTitle');
}
}
}
function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}
you also need to add a nonce value to prevent concurrency
add a hidden input in your form:
<input type="hidden" name="customCategory_noncename" id="customCategory_noncename" value="<?= wp_create_nonce('customCategory'); ?>" />
and add this to your save code
// verify this with nonce because save_post can be triggered at other times
if (!wp_verify_nonce($_POST['_noncename'], 'my_customHeader')) return $post_id;
also by default i think wordpress strips out html formatting in the editor in favour of its own 'smart' html tagging
I'm not exactly very well-versed with the Wordpress hooks related to saving posts, but based on your PHP alone, I'm seeing that your custom_add_save() function is not returning the post id when it's processing a manual save (i.e. when you click the Save Draft / Publish button on the Wordpress UI).
It's certainly returning the post id during an auto-save (as per the first block of code as you enter custom_add_save), though.
Maybe you'd like to look into that. :)

Categories