Show custom field if it exists, and show different elements if it doesn’t

Inside the loop, you can check the custom field with get_post_meta. Like this. If custom field randomname exists then it will show it’s value otherwise it will print <div class=”name”>content</div> <?php if ( get_post_meta( $post->ID, ‘randomname’, true ) ) { echo get_post_meta( $post->ID, ‘randomname’, true ); } else { echo ‘<div class=”name”>content</div>’; } ?> OR … Read more

Plugin not saving values when placed in a folder within /wp-content/plugins

Here is the fixed and modified code which was having the nonce action issue. <?php /* Plugin Name: Test Plugin Plugin URI: Test Description: Test Author: Test Version: 007 Author URI: */ // add all post types $clinical_post_types = array(); $post_types = get_post_types(); foreach ( $post_types as $post_type ) { $clinical_post_types[] = $post_type; } // … Read more

Sorting posts by custom fields in meta_query

As of WordPress 4.2, you can combine meta_query and orderby. The following example is taken from make.wordpress.org. Example of a simple meta_query: $q = new WP_Query( array( ‘meta_query’ => array( ‘relation’ => ‘AND’, ‘state_clause’ => array( ‘key’ => ‘state’, ‘value’ => ‘Wisconsin’, ), ‘city_clause’ => array( ‘key’ => ‘city’, ‘compare’ => ‘EXISTS’, ), ), ‘orderby’ … Read more

wp_editor() usage in custom meta box

This may not solve the problem, but I hope it helps you. In the sky_post_excerpts() function, I used this to display the TinyMCE/classic editor: (the $data is get_post_meta($post->ID, ‘skyscraper_post’, true)) <?php wp_editor( $data, ‘post_meta_box’, array(‘textarea_name’=>’skyscraper_post’)); ?> In the post_meta_box_save() function, I saved the meta like so, where $allowed is wp_kses_allowed_html(): update_post_meta( $post_id, ‘skyscraper_post’, wp_kses( $_POST[‘skyscraper_post’], … Read more

Escaping Quotes

WordPress emulates magic_quotes_gpc and won’t be deprecating this for some time due to concerns over plugin security and backward compatibility. To work around this, you need to use stripslashes() or, if you need to strip slashes when data arrives via $_POST, $_GET, $_COOKIE, and $_REQUEST arrays, you can use the WordPress stripslashes_deep() function: http://codex.wordpress.org/Function_Reference/stripslashes_deep