admin管理员组

文章数量:1278947

I am adding the following code to the end of my functions.php file to populate a taxonomy based on a Custom post's post-title. The problem is that when I add the code I get the following error trying to login on wp-admin. Any help figuring out why this is happening is greatly appreciated.

Error:

Error: Cookies are blocked due to unexpected output. For help, please see this documentation or try the support forums.

Code:




<?php

function update_custom_terms($post_id) {

  // only update terms if it's a 'artist' post
  if ( 'artist' != get_post_type($post_id)) {
    return;
  
  // don't create or update terms for system generated posts
  if (get_post_status($post_id) == 'auto-draft') {
    return;
  }
    
 
  $term_title = get_the_title($post_id);
  $term_slug = get_post( $post_id )->post_name;

  /*
  * Check if a corresponding term already exists by comparing 
  * the post ID to all existing term descriptions.
    'artists' is the taxonomy getting populated
  */
  $existing_terms = get_terms('artists', array('hide_empty' => false));

  foreach($existing_terms as $term) {
    if ($term->description == $post_id) {
      //term already exists, so update it and we're done
      wp_update_term($term->term_id, 'artists', array(
        'name' => $term_title,
        'slug' => $term_slug
        )
      );
      return;
    }
  }

  /* 
  * If we didn't find a match above, this is a new post, 
  * so create a new term.
  */
  wp_insert_term($term_title, 'artists', array(
    'slug' => $term_slug,
    'description' => $post_id
    )
  );
}

//run the update function whenever a post is created or edited
add_action('save_post', 'update_custom_terms');

function delete_term( $post_id ) {

  $post = get_post( $post_id );

  if ( term exists( $post->post_title, 'artists' ) ) {
    $term = get_term_by( 'name', $post->post_title, 'artists' );
    wp_delete_term( $term->term_id, 'artists' );
  }

}
add_action( 'before_delete_post', 'delete_term' );
?>

I am adding the following code to the end of my functions.php file to populate a taxonomy based on a Custom post's post-title. The problem is that when I add the code I get the following error trying to login on wp-admin. Any help figuring out why this is happening is greatly appreciated.

Error:

Error: Cookies are blocked due to unexpected output. For help, please see this documentation or try the support forums.

Code:




<?php

function update_custom_terms($post_id) {

  // only update terms if it's a 'artist' post
  if ( 'artist' != get_post_type($post_id)) {
    return;
  
  // don't create or update terms for system generated posts
  if (get_post_status($post_id) == 'auto-draft') {
    return;
  }
    
 
  $term_title = get_the_title($post_id);
  $term_slug = get_post( $post_id )->post_name;

  /*
  * Check if a corresponding term already exists by comparing 
  * the post ID to all existing term descriptions.
    'artists' is the taxonomy getting populated
  */
  $existing_terms = get_terms('artists', array('hide_empty' => false));

  foreach($existing_terms as $term) {
    if ($term->description == $post_id) {
      //term already exists, so update it and we're done
      wp_update_term($term->term_id, 'artists', array(
        'name' => $term_title,
        'slug' => $term_slug
        )
      );
      return;
    }
  }

  /* 
  * If we didn't find a match above, this is a new post, 
  * so create a new term.
  */
  wp_insert_term($term_title, 'artists', array(
    'slug' => $term_slug,
    'description' => $post_id
    )
  );
}

//run the update function whenever a post is created or edited
add_action('save_post', 'update_custom_terms');

function delete_term( $post_id ) {

  $post = get_post( $post_id );

  if ( term exists( $post->post_title, 'artists' ) ) {
    $term = get_term_by( 'name', $post->post_title, 'artists' );
    wp_delete_term( $term->term_id, 'artists' );
  }

}
add_action( 'before_delete_post', 'delete_term' );
?>
Share Improve this question asked Oct 29, 2021 at 15:45 Alex PAlex P 1
Add a comment  | 

1 Answer 1

Reset to default 0

It seems that the issue was not with the code itself but with ?php and ?> tags throughout the functions.php file. I copied all the contents into notepad++ and removed all the tags other than the top tag and the code works fine and I can now once again log in.

本文标签: Can39t log in to WordPress wpadmin after adding code to functionsphp