0

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
  • 1
  • 1
  • When you do `$uploadfiles = glob(...` it is going to overwrite whatever you get from `$uploadfiles = get_post_meta(...` What is the value of `$uploadfiles` after the `get_post_meta()` call? – Patrick Q Jul 08 '19 at 14:19
  • Patrick Q ...the word Array – Young Vic Jul 08 '19 at 14:31
  • Always check values (during debugging) using `var_dump()` not `echo` – Patrick Q Jul 08 '19 at 14:32
  • @Patrick Q i am on my first learning stages and may not know most things... I can't see the display of a button i had...let me check where i messed up and try var_dump() – Young Vic Jul 08 '19 at 15:22
  • @Patrick Q it gives me array(1) { [0]=> string(112) "http://localhost/wordpress/wp-content/uploads/job-manager-uploads/upload_files/2019/07/Hello-testing1-1.docx" } which is the actual file I am aiming for... – Young Vic Jul 08 '19 at 15:42
  • Then get rid of the `glob()` line. That should take care of your first issue. – Patrick Q Jul 08 '19 at 15:52
  • @Patrick Q yes awesome!!!! Thank you!!! the second issue is the whole url is visible instead of the file name only. What should i replace the second .$f. with? – Young Vic Jul 08 '19 at 16:12
  • You're going to have to do some string manipulation probably using a combination of [substr()](https://www.php.net/manual/en/function.substr.php) and [strrpos()](https://www.php.net/manual/en/function.strrpos.php). – Patrick Q Jul 08 '19 at 16:16
  • I may not know how to do that. Let me try and if I am unable I will inform you – Young Vic Jul 08 '19 at 16:54
  • Possible duplicate of [How do I get a file name from a full path with PHP?](https://stackoverflow.com/questions/1418193/how-do-i-get-a-file-name-from-a-full-path-with-php) – Patrick Q Jul 08 '19 at 17:06
  • @Patrick Q You are a life saver. Thank you!! I was able to do it with foreach($uploadfiles as $f) {$str = "$f"; $last = explode("/", $str); echo ''.$last[10].'
    ';
    – Young Vic Jul 08 '19 at 17:22
  • or foreach($uploadfiles as $f) {$str = "$f";$last = substr(strrchr($f, "/"), 1); echo ''.$last.'
    ';
    – Young Vic Jul 08 '19 at 17:39
  • Feel free to either accept the suggested duplicate or post an Answer with the code you ended up using – Patrick Q Jul 08 '19 at 17:41

0 Answers0