Home › Forums › Backend Issues (wp-admin) › Unable to get author
I’ve written this code, which pulls from an ACF gallery field called images (which returns an image array) and an unformatted textarea field called photo_notes, and everything works fine. The only problem is that when I download the ZIP, the filename should be the name of the post and the name of the author, but it always puts the name of the author as myself, even when I switch to a different account to create a post and download the images from a post.
This is all put into an Enhanced Message field (a message field that allows PHP) and is displayed on the backend like this:
<?php
$status = get_post_status();
if ($status == 'publish') {
?>
<button type="button" class="button" onclick="download()">Download assignment</button>
<script>
function download() {
var zip = new JSZip();
var img = zip.folder("<?php echo get_the_title(); echo " - "; echo get_the_author_meta('display_name'); ?>")
zip.file("assignment.txt", "<?php the_field( 'photo_notes' ); ?>");
<?php
$images = get_field('images');
if( $images ): ?>
function urlToPromise(url) {
return new Promise(function(resolve, reject) {
JSZipUtils.getBinaryContent(url, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
<?php foreach( $images as $image ): ?>
img.file("<?php echo rand(0,999999999999999) ?>.jpg", urlToPromise("<?php echo esc_url($image['url']); ?>"), {
binary: true
});
<?php endforeach; ?>
<?php endif; ?>
zip.generateAsync({
type: "blob"
})
.then(function(content) {
// Force down of the Zip file
saveAs(content, "<?php echo get_the_title(); echo " - "; echo get_the_author_meta('display_name'); ?>.zip");
});
}
</script>
<i>This may take a while, depending on the number of images and their size. If it's not working, you can manually download individual images from the gallery field below.</i>
<?php
} else {
?>
<button type="button" class="button" disabled>Download assignment</button>
<i>Please submit this assignment to download it.</i>
<?php
}
?>
The post is a custom post type called photo_assignment, which is configured with this code:
<?php
// Register Custom Post Type
function register_photo_assignment() {
$labels = array(
'name' => _x( 'Photo Assignments', 'Post Type General Name', 'photo_assignment' ),
'singular_name' => _x( 'Photo Assignment', 'Post Type Singular Name', 'photo_assignment' ),
'menu_name' => __( 'Assignments', 'photo_assignment' ),
'name_admin_bar' => __( 'Assignments', 'photo_assignment' ),
'archives' => __( 'Assignment Archives', 'photo_assignment' ),
'attributes' => __( 'Assignment Attributes', 'photo_assignment' ),
'parent_item_colon' => __( 'Parent Assignment:', 'photo_assignment' ),
'all_items' => __( 'All Assignments', 'photo_assignment' ),
'add_new_item' => __( 'New Assignment', 'photo_assignment' ),
'add_new' => __( 'Upload', 'photo_assignment' ),
'new_item' => __( 'Create Assignment', 'photo_assignment' ),
'edit_item' => __( 'Edit Assignment', 'photo_assignment' ),
'update_item' => __( 'Update Assignment', 'photo_assignment' ),
'view_item' => __( 'View Assignment', 'photo_assignment' ),
'view_items' => __( 'View Assignments', 'photo_assignment' ),
'search_items' => __( 'Search Assignments', 'photo_assignment' ),
'not_found' => __( 'No assignments found', 'photo_assignment' ),
'not_found_in_trash' => __( 'No assignments found in Trash', 'photo_assignment' ),
'featured_image' => __( 'Cover Image', 'photo_assignment' ),
'set_featured_image' => __( 'Set cover image', 'photo_assignment' ),
'remove_featured_image' => __( 'Remove cover image', 'photo_assignment' ),
'use_featured_image' => __( 'Use as cover image', 'photo_assignment' ),
'insert_into_item' => __( 'Insert into assignment', 'photo_assignment' ),
'uploaded_to_this_item' => __( 'Added to this assignment', 'photo_assignment' ),
'items_list' => __( 'Assignment list', 'photo_assignment' ),
'items_list_navigation' => __( 'Assignment list navigation', 'photo_assignment' ),
'filter_items_list' => __( 'Filter assignments list', 'photo_assignment' ),
);
$args = array(
'label' => __( 'Photo Assignment', 'photo_assignment' ),
'description' => __( 'A way to submit photo assignments', 'photo_assignment' ),
'labels' => $labels,
'supports' => array( 'title', 'thumbnail' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-camera',
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type( 'photo_assignment', $args );
}
add_action( 'init', 'register_photo_assignment', 0 );
// Change some stubborn text
function wps_translation_mangler($translation, $text, $domain) {
global $post;
if ( get_post_type() == 'photo_assignment' ) {
$translations = &get_translations_for_domain( $domain);
if ( $text == 'Publish') {
return $translations->translate( 'Submit' );
}
if ( $text == 'Draft') {
return $translations->translate( 'Unsubmitted' );
}
if ( $text == 'Move to Trash') {
return $translations->translate( 'Delete' );
}
if ( $text == 'Published') {
return $translations->translate( 'Submitted' );
}
if ( $text == 'Trash') {
return $translations->translate( 'Deleted' );
}
if ( $text == 'Submit for Review') {
return $translations->translate( 'Submit');
}
if ( $text == 'Published' ) {
return $translations->translate( 'Submitted' );
}
if ( $text == 'Save Draft' ) {
return $translations->translate( 'Submit Later' );
}
if ( $text == 'Add to gallery' ) {
return $translations->translate( 'Upload photos' );
}
}
return $translation;
}
add_filter('gettext', 'wps_translation_mangler', 10, 4);
?>
I was also told that loading PHP in the backend using the Enhanced Fields plugin is insecure. Is there a better way to do this?
This doesn’t seem to be an issue with ACF, rather you should contact the Enhanced Fields developer and see if you can get some insight there.
The topic ‘Unable to get author’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.