I am trying to: activate a custom code, called "copy-featured-image" content the following code:
function func_copy_featured_image_to_user_field( $post_id, $post ){
if ( 'profilo' == $post->post_type ) {
$author_user_id = $_POST['post_author'];
// $thubnail_id = $_POST['_thumbnail_id'];
$featured_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
update_user_meta($author_user_id, 'ihc_avatar ', $featured_url);
}
}
add_action( 'save_post', 'func_copy_featured_image_to_user_field', 30, 2 );
This code permitted me to synchronize the Users avatar, the ihc_avatar image field, and the Profilo CPT post featured image.
This code worked until one month ago.
Link to a page where the issue can be seen: lien caché
I expected to see: The Custom Code activated and in Run
Instead, I got: When I try to activate the code I have this message "Si è verificato un errore durante l'aggiornamento dei frammenti.". And the code not are activated.
Then there is an error in your code, I would say on the first spot, but I could not see any obvious errors.
Maybe your fields are not defined?
1. Important to know would be when this started to fail
2. To test what is wrong you can do this:
- remove everything from within the code and save only the wrapper:
function func_copy_featured_image_to_user_field( $post_id, $post ){
//nothing
add_action( 'save_post', 'func_copy_featured_image_to_user_field', 30, 2 );
- if it works start to add line by line of your code
- if it does not work, send me a copy of that site (because this code works in its entirety on my local test)
The only what I saw is a warning which is immediately solved as soon I add the security check at the begin of the snippet:
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
Thanks for the reply.
So, if I try to activate only this code it is activated without any issue:
function func_copy_featured_image_to_user_field( $post_id, $post ){
//nothing
add_action( 'save_post', 'func_copy_featured_image_to_user_field', 30, 2 );
If I try to activate the following code, I have the issue, and I don't know why...:
function func_copy_featured_image_to_user_field( $post_id, $post ){
if ( 'profilo' == $post->post_type ) {
}
}
add_action( 'save_post', 'func_copy_featured_image_to_user_field', 30, 2 );
No issue activating that code on my local install, and also I see no reason as of why it should fail, that code just checks if the $post->post_type is == profilo
Please share the access to the site so I can have a look, and if possible, could you recall when this started to happen?
See https://toolset.com/forums/topic/i-cannot-activate-one-custom-code/#post-1221499
Also running the code tells me
- Snippet "test" has been updated.
- The snippet has been re-run without errors.
Plese make sure this is not due to duplicated code, or other sofwtware interfering or a theme that is causing it.
Hi, I was trying to check your site's error_log.txt file, but it's over 700Mb. Then the site refused to connect, and now I see an error. Can you check?
Hi Christian,
Try to check now.
I don't see any error!
Thanks
I'm not able to create any new code snippets either - the same error is displayed. I have a few ideas:
- Check your server disk space. Perhaps you are running out of storage, and the system will not allow you to add new files in this directory.
- Check file permissions (you may need your host to help with this). Make sure WordPress has permissions to write and edit files and directories under the wp-content folder.
Hi Christian,
so, it is strange. The hosting give me up to 100GB and all network use only 20GB. To this site, I have assigned 10GB, and it is not possible that it uses all 10GB...
The wp-content permissions are set to 755, and today I have created 3 new custom code from Toolset custom code section...
I don't know what should I do to help you in this case...
There seems to be a problem writing any snippet that includes the $_POST superglobal. I created a new snippet toolset-test-snippet-2, and added this code:
<?php
/**
* New custom code snippet.
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
function ts_copy_featured_image_to_user_field( $post_id, $post ){
// foo
}
add_action( 'save_post', 'ts_copy_featured_image_to_user_field', 30, 2 );
The snippet saves successfully. Then I added $_POST to the comment, like this:
<?php
/**
* New custom code snippet.
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
function ts_copy_featured_image_to_user_field( $post_id, $post ){
// foo $_POST
}
add_action( 'save_post', 'ts_copy_featured_image_to_user_field', 30, 2 );
The snippet no longer saves successfully. This is completely valid code, which indicates that something is going on at the server level. Perhaps a security filter or module is preventing this text being written.
I have controlled on the server and there was activated the ModSecurity module.
I have deactivated it and now I can activate the Custom Code that I need.
Can you try to test if it is true?
Thanks
If you're able to save the custom code snippet, then as far as I'm concerned the issue is resolved.