Add image to post from external URL

For those who would know : $upload_dir = wp_upload_dir(); $image_data = file_get_contents($image_url); $filename = basename($image_url); if(wp_mkdir_p($upload_dir[‘path’])) $file = $upload_dir[‘path’] . “https://wordpress.stackexchange.com/” . $filename; else $file = $upload_dir[‘basedir’] . “https://wordpress.stackexchange.com/” . $filename; file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( ‘post_mime_type’ => $wp_filetype[‘type’], ‘post_title’ => sanitize_file_name($filename), ‘post_content’ => ”, ‘post_status’ => ‘inherit’ ); $attach_id … Read more

How to pass both action and formdata in wordpress ajax?

try to add the action in the formdata : var formData=new FormData(document.getElementById(‘landlordregform’)); formData.append(“action”, “custom_landlord_registration_process”); jQuery.ajax({ url: jQuery(“#cusajaxurl”).val(), type: ‘POST’, data: formData, cache: false, processData: false, contentType: false, success: function(data) { console.log(data); } });

How to link WordPress heartbeat to ajax form

Saving data Based on Heartbeat API, you can add custom data to the sent heartbeat on heartbeat-send event in js. This would be used instead of the ajax function you have in your code example. jQuery( document ).on( ‘heartbeat-send’, function ( event, data ) { // Grab the required form data data.location = jQuery(‘input[name=”location”]:checked’).val(); data.userid … Read more

Can I load posts via Ajax?

Absolutely! There are a number of themes on the market that do load posts via AJAX. Take a look at any of them to get an idea of how they work. A particularly good example is K2 – K2 loads entire pages of posts via AJAX if you click on the previous posts link. To … Read more

jQuery Autocomplete in WordPress

don’t include WP like that. use $_GET instead: … $(“#tags”).autocomplete(“<?php echo add_query_arg(‘get_my’, ‘terms’, home_url()); ?>”, … theme’s functions.php: add_action(‘template_redirect’, ‘terms_for_autocomplete’); function terms_for_autocomplete(){ if(isset($_GET[‘get_my’]) && $_GET[‘get_my’] == ‘terms’): $terms = &get_terms(get_taxonomies()); foreach ($terms as $term) echo “{$term->name}|{$term->name} ({$term->count} results)\n”; die(); endif; }

problem with ajax and the path to the php page

To use the WordPress ajax url you can pass the var using wp_localize_script: wp_enqueue_script( ‘functions’, get_bloginfo( ‘stylesheet_directory’ ) . ‘/js/functions.js’, array( ‘jquery’ ), false); wp_localize_script( ‘functions’, ‘MyAjax’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ) ) ); In your functions.js $.ajax({ type: “POST”, url: MyAjax.ajaxurl, data: {p:alt_p}, success: function(data) { alert(data); } });

Conditional check for front-end which includes ajax

DOING_AJAX and WP_ADMIN (this constant is checked within is_admin() ) are defined in admin-ajax.php and set tro true. Both are not very helpfull in this situation. Add a parameter to your ajax request if the request come from the front-end: <?php function my_action_javascript() { ?> <script type=”text/javascript” > jQuery(document).ready(function($) { var data = { action: … Read more