1

I have a series of themes that allow site visitors to contribute content as posts (they enter as drafts) from a front end editor. I have been using a technique of invisibly logging them in as an author user for access to media uploader, and using a plugin to prevent access to the dashboard.

I am trying to update w/o doing any kind of login, and have a working version doing uploads to the media library with dropzone.js

My limit now is that I had found that in my earlier versions by enqueuing mce-views that the front end editor would render previews in the editor content WordPress can autoembed. But without being logged in, I no longer get autoembeds, only when logged in. I have compared the scripts loaded, and only found 3 that were loaded when logged in that were not w/o a login. Enqueuing them made no difference.

The editor is placed in a form via:

<fieldset id="theText">
        <label for="wText">Write Here</label>
        <?php
        // set up for inserting the WP post editor
        $settings = array( 
            'textarea_name' => 'wText', 
            'editor_height' => '400'
        );

        wp_editor(  stripslashes( $wText ), 'wtext', $settings );
        ?>
</fieldset>

The value of $wText is loaded if the form has been submitted (e.g. if it generated a validation error on first pass).

My form is used on a single page, so my function hooked into the action wp_enqueue_scripts loads these scripts just for this page:

if ( is_page( truwriter_get_write_page() ) ) { // use on just our form page

    // Build in tag auto complete script
    wp_enqueue_script( 'suggest' );

    //dropzone.js for file uploads
    wp_enqueue_script( 'dropzone', get_stylesheet_directory_uri() . '/js/dropzone.js', '', '5.5', true );
    wp_enqueue_style( 'dropzone-style', get_stylesheet_directory_uri() . '/js/css/dropzone.css' );

    // Autoembed functionality in rich text editor
    // needs dependency on tiny_mce
    // h/t https://wordpress.stackexchange.com/a/287623

    wp_enqueue_script( 'mce-view', '', array('tiny_mce'), '', true );   

    // custom jquery for the uploader on the form
    wp_register_script( 'jquery.writer' , get_stylesheet_directory_uri() . '/js/jquery.writer.js', array( 'suggest', 'dropzone') , '1.9', TRUE );

    // add a local variables for the site's home url, ajax address, and maximum upload size
    wp_localize_script(
      'jquery.writer',
      'writerObject',
      array(
        'siteUrl' => esc_url(home_url()),
        'ajaxUrl' => admin_url('admin-ajax.php'),
        'uploadMax' => round(wp_max_upload_size() / 1000000)
      )
    );

    wp_enqueue_script( 'jquery.writer' );
}

In viewing the sequence in Chrome inspector, when logged in and the autoembed works, there is a call to admin_ajax.php -- this of course will fail when not logged in, and this is my best guess as to why it's not working.

The value for ajaxUrl passed to the js script is to enable the tags autocomplete capability, and no surprise, it does not work either when not logged in.

jQuery('#wTags').suggest( writerObject.ajaxUrl + "?action=ajax-tag-search&tax=post_tag", {multiple:true, multipleSep: ","});

I know from research that to enable ajax calls I need to have an action set up like wp_ajax_nopriv_{action} (I in fact do this successfully for the dropzone.js integration since I wrote the functions that are called back by ajax.

But I have no idea what callbacks are needed to be granted wp_ajax_nopriv_{action} these are not my functions but from internal WordPress code for doing oembed and the auto tag suggest.

So (a) is it even possible to enable auto embeds of media on a front end editor w/o a login and/or (b) how to I grant access to admin-ajax.php if there is no login?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.