WP WordPress theme issue - php

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' ) ) {

Related

Change the content with the url on php?

I have a file named friend.php and I want change the friend.php content doing this:
friend.php?content=1, friend.php?content=2
Searching on google I can find change elements from url but I want change the content complete for example have content1.php, content2.php... but show that only doing this with the url.
Access this using $_GET superglobal:
<?php
if ($_GET["content"] == "1") {
// show content1.php
} else if ($_GET["content"] == "2") {
// show content2.php
}
?>

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/Jetpack: What part of my code is causing Jetpack to break?

I have created a custom post type (images) for handling event images, to which I have also added a custom image uploader meta field, and everything works exactly the way I want it to, EXCEPT, it breaks jetpack. I know from experience with Jetpack that a plugin may create unexpected output, causing the "-32700" error with jetpack, but i don't know which part of the code is causing it. The code for the image uploader is:
<?php
function add_custom_meta_boxes() {
// Define the custom attachment for posts
add_meta_box(
'wp_image_attachment',
'Custom Attachment',
'wp_image_attachment',
'images',
'side'
);
} // end add_custom_meta_boxes
add_action('add_meta_boxes', 'add_custom_meta_boxes');
function wp_image_attachment() {
wp_nonce_field(plugin_basename(__FILE__), 'wp_image_attachment_nonce');
$html = '<p class="description">';
$html .= 'Upload your image here.';
$html .= '</p>';
$html .= '<input type="file" id="wp_image_attachment" name="wp_image_attachment" value="" size="25">';
echo $html;
} // end wp_image_attachment
function save_custom_meta_data($id) {
/* --- security verification --- */
if(!wp_verify_nonce($_POST['wp_image_attachment_nonce'], plugin_basename(__FILE__))) {
return $id;
} // end if
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $id;
} // end if
if('page' == $_POST['post_type']) {
if(!current_user_can('edit_page', $id)) {
return $id;
} // end if
} else {
if(!current_user_can('edit_page', $id)) {
return $id;
} // end if
} // end if
/* - end security verification - */
// Make sure the file array isn't empty
if(!empty($_FILES['wp_image_attachment']['name'])) {
// Setup the array of supported file types. In this case, it's just PDF.
$supported_types = array('image/gif', 'image/jpeg', 'image/jpg', 'image/png');
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($_FILES['wp_image_attachment']['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if(in_array($uploaded_type, $supported_types)) {
// Use the WordPress API to upload the file
$upload = wp_upload_bits($_FILES['wp_image_attachment']['name'], null, file_get_contents($_FILES['wp_image_attachment']['tmp_name']));
if(isset($upload['error']) && $upload['error'] != 0) {
wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
} else {
add_post_meta($id, 'wp_image_attachment', $upload);
update_post_meta($id, 'wp_image_attachment', $upload);
} // end if/else
} else {
wp_die("The file type that you've uploaded is not an image.");
} // end if/else
} // end if
} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');
function update_edit_form() {
echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');
?>
I know that's a lot of code to be putting here, but I got shouted at last time I linked to pastebin [ ;) ], but any help would be greatly appreciated, as the site is nearly finished, but i'd really like to utilize the Publicize feature of Jetpack, so i'd prefer not to sacrifice it, but I NEED this bit to work.
Many thanks in advance! :)
Booooo! Rookie error! The unexpected output seems to have been caused by that section of my plugin being added onto the original custom post type section, which, of course ended with
?>
and the above section started with
<?php
which wordpress didn't seem to like.
Problem solved. For now...
*NB: This can probably be deleted now, or left for future reference :) *

Make php do_action conditional upon class field

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.
}
?>

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