WordPress Custom Page Template in a different directory

WordPress uses get_page_template() to determine the template file to use which can be altered by filters: //for pages add_filter( ‘page_template’, ‘My_custom_page_template’ ); function My_custom_page_template( $page_template ) { if ( is_page( ‘my-custom-page-slug’ ) ) { $page_template=”pathto/custom-page-template.php”; } return $page_template; } //for posts add_filter( ‘single_template’, ‘My_custom_page_template’ ); function My_custom_page_template( $single_template ) { if ( is_single( ‘my-custom-page-slug’ ) … Read more

media_handle_upload weird thing

Look into the first lines of this function: function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( ‘test_form’ => false ) ) { $time = current_time(‘mysql’); if ( $post = get_post($post_id) ) { if ( substr( $post->post_date, 0, 4 ) > 0 ) $time = $post->post_date; } $name = $_FILES[$file_id][‘name’]; $file = wp_handle_upload($_FILES[$file_id], $overrides, … Read more

Using Cookie Data For WP_Query Loop

You need to explode the comma separated string to get the array of IDs. Follow the below code. $cookie_array = $_COOKIE[“your-results”]; // Test output echo $cookie_array; $cookie_array = array_map( ‘absint’, (array) explode(‘,’, $cookie_array) ); $sug_args = array( ‘post_type’ => ‘product’, ‘post__in’ => $cookie_array, ); $sug_query = new WP_query ($sug_args); if( $sug_query->have_posts() ) : while( $sug_query->have_posts() … Read more

How to load WP functions?

The root problem here is that you should never make direct requests to PHP files in a theme or plugins from the browser. It is extreme bad practice, fragile, and open to lots of security issues. To get this working as is you would need to bootstrap WordPress by putting something similar this near the … Read more

Display only text to WordPress loop without loosing the text formatting

Try this code in your theme’s functions.php file- function filtered_content($content){ $content = preg_replace(“/<img[^>]+\>/i”, “”, $content); // removes images $content = preg_replace(‘/<iframe.*?>/’, “”, $content); // removes iframes return $content; } add_filter(‘the_content’, ‘filtered_content’); This will remove img and iframe tag from your content. Then use the_content() in your template.

Limit the Excerpt field in WP-Admin in words

You can use something like jQuery Simply Countable plugin and attach it to excerpt input. Limit_Excerpt_Words::on_load(); class Limit_Excerpt_Words { static function on_load() { add_action( ‘admin_enqueue_scripts’, array( __CLASS__, ‘admin_enqueue_scripts’ ) ); } static function admin_enqueue_scripts() { global $hook_suffix; if ( ‘post.php’ == $hook_suffix || ‘post-new.php’ == $hook_suffix ) { wp_enqueue_script( ‘jquery-simply-countable’, plugins_url( ‘/jquery.simplyCountable.js’, __FILE__ ), array( … Read more