Accessing a random image via ajax

There is an error in your src; <img src=”http://example.com//wp-admin/admin-ajax.php?action=random_banner”> Try this one: <img src=”http://example.com/wp-admin/admin-ajax.php?action=random_banner”> or <img src=”https://wordpress.stackexchange.com/questions/215158/<?php echo admin_url(“admin-ajax.php?action=random_banner’ ); ?>”> You can also use a query to get a random image. AJAX add_action( ‘wp_ajax_random_banner’, ‘random_banner’ ); add_action( ‘wp_ajax_nopriv_random_banner’, ‘random_banner’ ); function random_banner() { // search for 1 random image $image_ids = get_posts( array( ‘post_type’ … Read more

Audio Player not loading when the content is loaded through Ajax, MediaElement.js no applied

I had the same problem. You need to reclassify the mediaelement style and script, like it is explained in this post. So just call this funtion, after your ajax call: function enqueue_mediaelement(){ wp_enqueue_style( ‘wp-mediaelement’ ); wp_enqueue_script( ‘wp-mediaelement’ ); } add_action( ‘wp_enqueue_scripts’, ‘my_enqueue_scripts’ ); If you wanna add your own styles for specific pages (in the … Read more

How to add a featured image to a existing post via php?

The trick is media_sideload_image() and set_post_thumbnail(). media_sideload_image() assumes you can grab the URL to the image, whether it exist in /wp-content/ or somewhere else (another site, even). As long as you can programmatically reference the image’s URL, something like this should work. $image=”image.jpg”; $media = media_sideload_image($image, $post->ID); if(!empty($media) && !is_wp_error($media)){ $args = array( ‘post_type’ => … Read more

Error in WP_update_post

The key to using WordPress outside of WordPress is include the wp-load.php file: So your code will like: <?php // Include the wp-load’ include(‘YOUR_WP_PATH/wp-load.php’); // Update post 1 hello word $my_post = array( ‘ID’ => 1, ‘post_title’ => ‘This is the updated post title.’, ‘post_content’ => ‘This is the updated content.’, ); // Update the … Read more

Limit the_excerpt with max of x characters

add_filter(‘wp_trim_excerpt’, function($text){ $max_length = 140; if(mb_strlen($text, ‘UTF-8’) > $max_length){ $split_pos = mb_strpos(wordwrap($text, $max_length), “\n”, 0, ‘UTF-8’); $text = mb_substr($text, 0, $split_pos, ‘UTF-8′); } return $text; }); This should take into account your max length and split the text at the nearest word boundary. Apply the filter, and call the_excerpt(); in your templates Apparently there’s a … Read more

Passing PHP Variables to JS using Localize Script

Your code should be in an action callback function: function wpse186202_enqueue_scripts(){ wp_enqueue_script( ‘stats’, get_stylesheet_directory_uri() .’/js/t5-demo.js’ , array( ‘jquery’ ), ‘1.0.0’, true ); $categories = implode( ‘, ‘, wp_list_pluck( get_the_category( get_the_ID() ), ‘name’ ) ); $datatoBePassed = array( ‘author’ => get_queried_object()->post_author, ‘category’ => $categories, ‘title’ => single_post_title( ”, false ) ); wp_localize_script( ‘stats’, ‘php_vars’, $datatoBePassed ); … Read more

Get the title before comma

Use strtok(): return strtok( get_the_title(), ‘,’ ); You could also use my function utf8_truncate(). This is a little bit more flexible and it can handle long strings without a comma.