4

Normally when we sort unattached images, they include logos and header images too. My question is simple. Is there any method to keep header and logos safe from deletion?

Is it possible to attach them to a page (eg logo or header page) automatically when uploaded so that they will not appear unattached in media library.

  • How exactly they are implemented? – Rarst Mar 21 '14 at 21:10
  • Using wordpress custom headers. codex.wordpress.org/Custom_Headers – wp student Mar 22 '14 at 1:51
  • Can you please also specify where the logos are implemented? This seems to be a feature of your theme :) – Sven Mar 22 '14 at 5:38
  • Logos are implemented in site header. :D – wp student Mar 22 '14 at 10:26
  • Please let me know the name of your current theme so I can have a look :) – Sven Mar 22 '14 at 13:28
2

You could add an ID as post_parent to the attachment of the header image via wp_update_post() (even though this seems to be a very very hacky way to do it!)

The tricky part is to get the ID out of the attachment URL; fortunately Rarst solved this issue long time ago, so you can manually add get_attachment_id() to your functions.

Next you'll have to assign an ID as post_parent; everytime you'll save the header image the selected header image will be attached to this special ID.

add_action( 'admin_init', 'attach_header_image' );
function attach_header_image() {
  if ( isset( $_POST['default-header'] ) ) :
    $header_image_url = get_header_image();
    $header_image_id = get_attachment_id( $header_image_url ); // function via https://wordpress.stackexchange.com/a/7094/32946
    $args = array(
      'ID' => $header_image_id,
      'post_parent' => 1 // assign header image to this ID
    );
    wp_update_post( $args );
  endif;
}

Nevertheless this seems to be a tricky way to solve the issue and it would probably superior to write an exception for the delete function...

Debugging Infos:

  • echo get_header_image() should output the link to the current header image URL (only true if a header image is defined)
  • echo get_attachment_id( $header_image_url ) should output the ID of the attachment page which should be equal to the ID of the attachment page you can see in Media (/wp-admin/post.php?post=123&action=edit); make also sure to copy and paste get_attachment_id() function from Rarst to your functions!
  • the if-statement containing default-header should check for the name of the checked input header field, which will be saved via $_POST enter image description here
  • Ok, I tested this function for header images, It didn't work. But later I tested it for front-end images just to ensure if something missing or wrong. But It worked perfectly. I think problem is with either default-header or get_header_image. I'll check into it. – wp student Mar 22 '14 at 10:32
  • @Waqas: I added some debugging infos to the answer! Let me know if it works for you! :) – Sven Mar 22 '14 at 13:19
  • 1
    O snap! error was in my head. I wasn't hitting "Save Changes" button after uploading. :D – wp student Mar 22 '14 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.