• Hello,

    I am managing one website. When I create a post i will enter tag keywords in tag section of post in WordPress. When i save the post I need php/javascript function to get the current post’s tags and create hyperlinks in the content and should save it to database. when a normal user access the post he should see the hyperlinks for the tags of that post. I am using generatepress child theme. I have searched internet, or other platforms But i did not get the code. tag hyperlink creation only happens at admin side when a post saved to database. It should not impact the performance when a normal user access the website and reading the posts.

    The below code I tried it always gives 500 internal server error. Can anyone suggest whats wrong in this code and rectify?

    <?php {
    
    function save_post_content_with_hyperlinks( $post_id, $post_content ) {
    // Check if the user is an admin.
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    
    // Check if the post ID is null.
    if ( is_null( $post_id ) ) {
        return;
    }
    
    // Check if the post content is null.
    if ( is_null( $post_content ) ) {
        return;
    }
    
    // Get the post tags.
    $post_tags = get_the_tags( $post_id );
    
    // Check if the post tags are null.
    if ( is_null( $post_tags ) ) {
        return;
    }
    
    // Create an array of hyperlinked tag keywords.
    $hyperlinked_tags = array();
    foreach ( $post_tags as $tag ) {
    
        // Check if the tag is null.
        if ( is_null( $tag ) ) {
            continue;
        }
    
        $hyperlinked_tag = null;
        if ( ! is_null( $tag ) ) {
            $hyperlinked_tag = '<a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a>';
        }
        $hyperlinked_tags[] = $hyperlinked_tag;
    }
    
    // Replace the tag keywords with hyperlinked tag keywords in the post content.
    $post_content = str_replace( $post_tags, $hyperlinked_tags, $post_content );
    
    // Update the post content in the database.
    wp_update_post( array(
        'ID'           => $post_id,
        'post_content' => $post_content,
    ) );
    
    // Return the post content with hyperlinks.
    return $post_content;
    }
    add_filter( 'save_post', 'create_hyperlinks_for_tag_keywords', 10, 2 );

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The first { after <?php likely does not belong there. Try removing it and see if that helps with the 500 error.

    If you still have trouble, you could try verifying your code at https://phpcodechecker.com/

    Thread Starter sam1981

    (@sam1981)

    The first { after <?php likely does not belong there . Yes That it typo. Please ignore that syntax error. Kindly focus on logic.

    • This reply was modified 2 years, 7 months ago by sam1981.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Tag keywords hyper link creation’ is closed to new replies.