Freelancealot
Forum Replies Created
-
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileHi,
Nope, didn’t add the id to the file name.
add_filter( 'wp_insert_post', 'foo_insert_post'); function foo_insert_post( $post_id ){ $post = get_post($post_id); if( 'submitted_paper' === $post->post_type ){ add_filter( 'plupload_default_params', 'foo_plupload_config'); } } function foo_plupload_config($params){ global $post; //assign current post id $params['post_id'] = $post->ID; return $params; } add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file) { // Get the parent post ID, if there is one if( isset($_REQUEST['post_id']) ) { $post_id = $_REQUEST['post_id']; } else { $post_id = false; } $file['name'] = $post_id . '-' . $file['name']; return $file; }
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileSame error as the first one
Notice: Trying to get property ‘post_type’ of non-object in /home/customer/www/***/public_html/wp-content/themes/bafa2021/functions.php on line 175
In the debug log it says
[23-Sep-2021 00:50:53 UTC] PHP Notice: Trying to get property ‘post_type’ of non-object in /home/customer/www/***/public_html/wp-content/themes/bafa2021/functions.php on line 175
[23-Sep-2021 00:51:02 UTC] int(507)I checked and the id of the post just created was 507!
But it did not add the 507 to the file name when I check in uploads/papers
Cheers,
TracyForum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileThe second one results in Parse error: syntax error, unexpected ‘if’ (T_IF) in /home/customer/www/***/public_html/wp-content/themes/bafa2021/functions.php on line 175
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileHi
The first one throws up this errorNotice: Trying to get property ‘post_type’ of non-object in /home/customer/www/***/public_html/wp-content/themes/bafa2021/functions.php on line 170
And does not add the id to the file name.
Line 170 in the functions.php is;
if( 'submitted_paper' === $post->post_type ){
Does that give you any clues? If not, I’ll try the second one.
Cheers,
TracyForum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileThere was a lot!
[22-Sep-2021 21:05:01 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function foo_insert_post(), 1 passed in /home/customer/www/ ****/public_html/wp-includes/class-wp-hook.php on line 305 and exactly 3 expected in /home/customer/www/****/public_html/wp-content/themes/bafa2021/functions.php:168 Stack trace: #0 /home/customer/www/***/public_html/wp-includes/class-wp-hook.php(305): foo_insert_post(485) #1 /home/customer/www/***/public_html/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters('', Array) #2 /home/customer/www/***/public_html/wp-includes/plugin.php(470): WP_Hook->do_action(Array) #3 /home/customer/www/***/public_html/wp-includes/post.php(4479): do_action('wp_insert_post', 485, Object(WP_Post), false) #4 /home/customer/www/****/public_html/wp-content/plugins/acf-extended/includes/modules/forms-action-post.php(330): wp_insert_post(Array) #5 /home/customer/www/***/publ in /home/customer/www/***/public_html/wp-content/themes/bafa2021/functions.php on line 168 [22-Sep-2021 21:05:01 UTC] PHP Fatal error: Uncaught Error: Object of class WP_Error could not be converted to string in /home/customer/www/***/public_html/wp-content/plugins/sg-security/templates/error.php:111 Stack trace: #0 /home/customer/www/***/public_html/wp-content/plugins/sg-security/core/Helper/Helper.php(277): include() #1 /home/customer/www/***/public_html/wp-includes/functions.php(3599): SG_Security\Helper\Helper->custom_wp_die_callback(Object(WP_Error), '', Array) #2 /home/customer/www/***/public_html/wp-includes/class-wp-fatal-error-handler.php(233): wp_die(Object(WP_Error), '', Array) #3 /home/customer/www/***/public_html/wp-includes/class-wp-fatal-error-handler.php(152): WP_Fatal_Error_Handler->display_default_error_template(Array, Object(WP_Error)) #4 /home/customer/www/***/public_html/wp-includes/class-wp-fatal-error-handler.php(57): WP_Fatal_Error_Handler->display_error_template(Array, Object(WP_Error)) #5 [internal funct in /home/customer/www/***/public_html/wp-content/plugins/sg-security/templates/error.php on line 111
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded file[“_acf_post_id”]=> string(3) “178”
The 178 is just a custom field value. It is not the post id or attachment id, it’s the id of an event the CPT is related to. So not useable.
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileMy latest comment has been held for moderation. Don’t know why. Hopefully it will be posted. Basically the debug log outputted the info for the form filled out to create the CPT and included the file path.
Cheers,
TracyForum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileI had this:
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file) { ob_start(); var_dump($_REQUEST); $content = ob_get_contents(); ob_end_clean(); error_log($content); // Get the parent post ID, if there is one if( isset($_REQUEST['p']) ) { $post_id = $_REQUEST['p']; } else { $post_id = false; } $file['name'] = $post_id . '-' . $file['name']; return $file; }
Not sure that helped. It just outputted the ACF form info.
[22-Sep-2021 22:42:43 UTC] array(7) { ["_acf_screen"]=> string(9) "acfe_form" ["_acf_post_id"]=> string(3) "178" ["_acf_validation"]=> string(1) "1" ["_acf_form"]=> string(3216) "N1NVTWhUOGE1bTdrKy96QisyaUkwd0tJeXZ4U2Nka2RLOERGclhpK1RBRjM2Zm94RFFmdU9VUEFzdC9PVW4rOCtuVWcrRGhCcC9RZkQvRXM2NENTc0gvK1BNWDhSM3pwa1E0bmpqMkthYmxHRVRvNDZSU08vY2lnbkhrNE9yRUI5R3ZpUlFjRG1KbmVFWi9XQmM5eHVDSURMems2Q0pMbmR4aSsxWXFjbURaZExFY3E4QURHdUwrTVYzR3FzR1crMitRaG9hT3BhVzN0MWpQSWJzdTVTUE1oK01PVDlzTUgreFI4NWxSOUkzRWxXNXEwa0hRd0NCZnA3dXlGZWhON25kZ25WWk1wNWZFR2FLTHB0aGs1YzBkVllkWVk1eElGZEZwczJyd25lQU5VWExxYldUMzVyZmdJWDBrQk5qMlpNZk1QSzkvSmVJMkZSU1YxcE5KZHIxdGFqeFFEbytIVmMvaDhsSGtGQzBmMjNZajVpVXRhSUFzaDJqelIzZ05CR1ZGSkc3Y09EVXlCclF0andrU1IvaWFRN1FLbFc1Qm1YbS9FakRWWmN2bDJndjZzVVFlUFI1T2NnTHkycDVtejFYYWtyaFF1S3IrZllDNVV4eksvWEFPVVVUM2NEMzYrM05NekdseTRxVlljcUNVZWl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CtCc2RMQ2llNjVnVFhqbFRhcUM0cVMxNWFuOGNoS1BrclJyNDVUeUdhN2NSMjcyZ3MvbTBiS2VMM0tBNXpzWUJwbEwxZHlHQ3JhM1hOVTRVSnZnNE1tS1o0cG5ueTZVc2xDOFhqbDhPeDA1Mm5kSW10T3I0NEIzSkdCb1NMaVd5blR6WEk1bW1tMFNaQlNHYTlKbWZpaFJYczJaNDNzL2M3YjlKTVNMc0RZNndsWFg1SDRNTk9GTE1ZZlI2dHZ2S213Wms5Y3NKRU9zT0JYL1RISWx0aFNVVXFSMHVZRmJaSDFWUG9lUUZYaW5EMExydUFQdDVCYjYrOUltU0RaSGMrckl0TFYyQUxwNFQxaFcvelNSaTZrK09TOGV4YnRLblNXN0RMM3JzWHc9PTo6h6TXwvnVvwMDFGJRZzyq1Q==" ["_acf_nonce"]=> string(10) "bc014bcd8c" ["_acf_changed"]=> string(1) "1" ["acf"]=> array(10) { ["field_60fcb14deabc3"]=> string(3) "178" ["field_60ff9358bc4bc"]=> string(1) "3" ["field_611adf4408e08"]=> string(1) "0" ["field_60ff41659b95c"]=> array(5) { ["field_60ff41c39b95d"]=> string(2) "Dr" ["field_60ff42299b95e"]=> string(4) "BAFA" ["field_60ff5705919dc"]=> string(13) "Admin" ["field_60ff4641ff47c"]=> string(15) "Some University" ["field_60ff5782919e0"]=> string(25) "[email protected]" } ["field_611aa6e1e2ec8"]=> string(1) "0" ["field_60fb3115564a6"]=> string(46) "Blimey how many test paper can one do in a day" ["field_60fb3180564a7"]=> string(19) "blah blah blah blah" ["field_60fb325d564a9"]=> string(0) "" ["field_60fb350d6e015"]=> string(119) "url=C%3A%5Cfakepath%5C2019%20Certificate%20of%20Attendance%20-%20Vincent%20Tawiah.pdf&size=70427&type=application%2Fpdf" ["field_610781f10cba3"]=> string(17) "22 September 2021" } }
- This reply was modified 3 years, 5 months ago by Freelancealot.
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileThanks for posting your code. So, I tried with the [‘p’]
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file) { // Get the parent post ID, if there is one if( isset($_REQUEST['p']) ) { $post_id = $_REQUEST['p']; } else { $post_id = false; } $file['name'] = $post_id . '-' . $file['name']; return $file; }
Nothing was prepended to the file name.
I can only assume that if this exact code works for you on standard WP posts, then it’s an issue with the parent-post id not being saved for the CPT attachment/media file.
I saw someone suggest to make the CPT hierarchy = true. But I would have thought that was just for making parent CPT posts, and would have no effect on the attachment/media.
Any suggestions?
Cheers,
TracyForum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileAlso tried the following but nothing got added to the file name:
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file){ if ( ! isset( $_REQUEST['post'] ) ) { return $file; } $id = intval( $_REQUEST['post'] ); $parent_post = get_post( $id ); $post_ref = $parent_post; // change file name $file['name'] = $post_ref . '-' . $file['name']; // return return $file; }
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileHi, I added your exact code ob_start(); etc to my functions.php file and I get the following when visiting one of the CPT posts:
[22-Sep-2021 20:53:38 UTC] array(3) { ["p"]=> string(3) "481" ["post_type"]=> string(15) "submitted_paper" ["preview_id"]=> string(3) "481" }
481 is the correct post id as I’m visiting the CPT submitted_paper. But how does that help get it appended to the attachment file name?
You say you were successful with post ‘with essentially the code you provided’, was the code the same or not. If not, what was the difference and would it help my case?
The code from the other post did not work. I got a FATAL ERROR
Uncaught ArgumentCountError: Too few arguments to function foo_insert_post()
So no luck yet.
Can’t believe no one else over all the years WordPress has been around hasn’t needed to do this ??
Thank you again for your time. Seems it’s an impossible thing to do with WordPress, which I think is a first for me.
Cheers,
TracyHi, Surprised more WordPress experts have not come back on this one. Is it too obvious or not possible?
Need to hook into the user creation process and add something like
user_login = first_name-last_name
But I am not a coder and really do need some help on this support forum if possible.
Cheers,
TracyForum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileIs there no way to use $parent_id = $media->post_parent; or some such. I can’t believe it’s so difficult to get this ID ??
or $parent = get_post_ancestors( $post->ID );
Help, really need to get this working.
- This reply was modified 3 years, 5 months ago by Freelancealot.
Forum: Fixing WordPress
In reply to: How to prepend parent post ID to uploaded fileHi @aetherunbound,
That did not work. Nothing gets prepended to the document name. My full code is:
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file) { // Get the parent post ID, if there is one if( isset($_REQUEST['post']) ) { $post_id = $_REQUEST['post']; } else { $post_id = false; } $file['name'] = $post_id . '-' . $file['name']; return $file; }
Does that look correct to you?
I am wondering if it’s to do with the issue I posted above ‘“Custom Post Types don’t store post_parent for attachments” and whether that is true.
To be honest the other stuff was right over my head. I am not a coder, just a snippet hacker ??
Once I get the media file’s parent post Id prepended to the file name, I can look at securing the code.
Cheers, and thanks for your time
TracyHi @plantprogrammer Thanks, but as per the title of the topic,’How to set First and Last Name as username‘. Perhaps I didn’t make the main text clear enough. I have already dealt with using First and Last Name for the Display Name. I need to hook into the Username to use those as well.