I have a plugin wp job manager, where we have already been shown how to add fields on a form. I have tested and added the salary field as shown here https://wpjobmanager.com/document/tutorial-adding-a-salary-field-for-jobs/ and it worked
Now I want to add an upload attachment field, where the logged in user posting the job on the form will upload files that will be displayed on the post listing and be downloaded by a logged in user viewing the listing
This is what i did
add_filter( 'submit_job_form_fields', 'frontend_add_salary_field' );
function frontend_add_salary_field( $fields ) {
$fields['job']['job_salary'] = array(
'label' => __( 'Total Salary ', 'job_manager' ),
'type' => 'text',
'required' => true,
'placeholder' => 'e.g. 20000',
'priority' => 6
);
return $fields;
}
and to display it on the post listing
add_action( 'single_job_listing_meta_end', 'display_job_salary_data' );
function display_job_salary_data() {
global $post;
$salary = get_post_meta( $post->ID, '_job_salary', true );
if ( $salary ) {
echo '<li>' . __( 'Total Salary:' ) . ' $' . esc_html( $salary )
.'</li>';}}
what i tried to insert the upload button and worked
add_filter( 'submit_job_form_fields', 'frontend_add_attachment_field' );
function frontend_add_attachment_field( $fields ) {
$fields['job']['job_attachment'] = array(
'label' => __( 'Upload Files','job_manager' ),
'type' => 'file',
'required' => false,
'placeholder' => '',
'priority' => 6,
'ajax' => true,
'multiple' => true,
'allowed_mime_types' => array(
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
);
return $fields;
}
This is where the problem is....The above upload button was able to help me upload files, but displaying them is the problem...
What i tried is
add_action( 'single_job_listing_meta_end', 'display_job_attachment_data'
);
function display_job_attachment_data() {
global $post;
$uploadfiles = get_post_meta( $post->ID, '_job_attachment', true );
$uploadfiles = glob('wp-content/uploads/job-manager-
uploads/upload_files/2019/07/*');
foreach($uploadfiles as $f) {
echo '<a href="http://localhost/wordpress/'.$f.'">'.$f.'</a><br />';
The display works, but not how I want it to...
First problem, instead of getting the specific file the specific user uploaded, it is getting the entire files in the directory. Even those uploaded by other users.
second problem, i am seeing the whole path to the directory on the post listing instead of the file name only
What do I want?
I need the display code to help me get the specific files uploaded by the specific user using the upload form and be viewed and downloaaded by the user checking the post. And not show the path.
With Patrick Q's help, i removed glob() and ended up using
foreach($uploadfiles as $f) {
$str = "$f";
$last = substr(strrchr($f, "/"), 1);
echo '<a href="'.$f.'">'.$last.'</a><br />';
after the get_post_meta() Thank you!!
'; – Young Vic Jul 08 '19 at 17:22
'; – Young Vic Jul 08 '19 at 17:39