• Resolved codecks

    (@codecks)


    Hi,

    I’m using this plugin on a page with the [wp-dropzone] shortcode. Would it be possible to automatically create a new post after a successful send using the ‘callback’ attribute?

    I’m thinking something like this;

    [wp-dropzone callback="success: <function to create a new post including the uploaded picture"]

    Is it possible to do this in that way or has it to be done with some other method?

    The end goal is to allow registered users to upload a photo and having automatically a post being created containing that photo.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Just looking at the plugin page, it’s apparent that the callback parameter refers to a javascript function. This could be an AJAX call to trigger the post creation, however, it would make more sense if there were an action in the plugin that is triggered when the image is added to the database.

    Thread Starter codecks

    (@codecks)

    Thanks for your reply Joy. I’ve been trying to use the callback to trigger the post creation but with no success so far.

    Basically what I have been doing is to create a custom-page template where I inserted the do_shortcode funtion to display the wp-dropzone form. The form works, and I’m able to upload files to the media library. The code looks like this:

    add_action('genesis_before_entry_content', 'dropit');
    function dropit()
    {
        echo do_shortcode('[wp-dropzone callback="success: function(file, response){ 
        console.log(response) }" desc="Drop your images here"]');
    }

    You can see the callback has a success event. So, on success it executes what’s inside. The content now function(file, response){ console.log(response) } is simply the example from the plugin instructions page. I’m not sure what I should replace it with so it triggers function to create a blog post.

    The function to create a blog post looks like this. It currently resides inside my custom page template, but I’m not sure if this is the good location, maybe functions.php would be a better location?

    add_action( 'wp_ajax_fotoblog', 'fotoblog' );
    function fotoblog()
    {
        $post = array(
        'post_title'    => 'My Title',
        'post_content'  => 'My Content',
        'post_status'   => 'publish',
        'post_type'     => 'post'
        );
        wp_insert_post($post);
        wp_die();
    }

    What I’m failing to grasp is the relationsship between the callback and the function to create a blog post. Of what I have been reading so far, I think I should create a javascript function that triggers the wp_ajax hook so it fires the post creation, but I might be wrong in my understanding of that?

    Check this carefully

    Shortcode
    [wp-dropzone callback="success: function(file, response) {var xhttp = new XMLHttpRequest(); xhttp.open('GET', 'https://site_url/wp-admin/admin-ajax.php?action=fotoblog', true); xhttp.send();}"]

    functions.php

    add_action( 'wp_ajax_fotoblog', 'fotoblog' );
    function fotoblog()
    {
        $post = array(
        'post_title'    => 'My Title',
        'post_content'  => 'My Content',
        'post_status'   => 'publish',
        'post_type'     => 'post'
        );
        wp_insert_post($post);
        wp_die();
    }
    Thread Starter codecks

    (@codecks)

    Hey Nazmul,

    Thank you so much, this is working!

    Is there a way to pass the response.data (url of the photo) as an argument to the fotoblog() function? I’m looking online on how to do this but currently this way above my level of understanding.

    Use POST method instead of GET. and pass object data inside send method.

    Example:
    [wp-dropzone callback="success: function(file, response) {var xhttp = new XMLHttpRequest(); xhttp.open('POST', 'https://site_url/wp-admin/admin-ajax.php?action=fotoblog', true); xhttp.send(response.data);}"]

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create new post after upload’ is closed to new replies.