Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:53:51+00:00 2026-06-12T15:53:51+00:00

I m trying to add the first link from the post content to a

  • 0

I m trying to add the first link from the post content to a custom field. Something is wrong and I ve been trying to get it to work but no luck. What should I do to add the link to “link” custom field?

add_action( 'publish_post', 'check_post' );
function check_post( $post_id ) {
$user_info = get_userdata(1);

    function get_first_link() {

        global $post, $posts;
        preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $links);
        return $links[1][0];

    }
    $first_link = get_first_link();

    add_post_meta($post_id, 'link', $first_link, true);
    add_post_meta($post_id, 'users', $user_info->user_login, true);
}

EDIT
I got it working halfway. It saves the url, but it doesn’t save it when the post is published. It saves it on update. What do I need to use? I tried using publish_post but that saved the url on update also.

add_action( 'save_post', 'my_save_post', 10, 2 );
function my_save_post( $post_id, $post ) {
if ( wp_is_post_revision( $post_id ) )
    return;

$matches = array();
preg_match_all( '/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $matches );

$first_link = false;
if ( ! empty( $matches[1][0] ) )
    $first_link = $matches[1][0];

$user_info = get_userdata(1);

$meta_link = $_POST['link'];

$args = array(
  'post__not_in'=> array($id),
  'post_type' => 'post',
  'post_status' => array('publish'),
  'meta_query' => array(

      array(
    'key' => 'link',
    'value' => $first_link,
    'compare' => '='
      )       
  )
);
$existingMeta = get_posts( $args );

if(empty($existingMeta)){
    //Go ahead and save meta data
        update_post_meta( $post_id, 'link', esc_url_raw( $first_link ) );
    update_post_meta($post_id, 'users', $user_info->user_login);
}else{
    //Revert post back to draft status
    update_post_meta( $post_id, 'link', $existingMeta[0]->ID );
        update_post_meta($existingMeta[0]->ID, 'users', $user_info->user_login);

    //Now perform checks to validate your data. 
       //Note custom fields (different from data in custom metaboxes!) 
       //will already have been saved.
        $prevent_publish= true;//Set to true if data was invalid.
        if ($prevent_publish) {
        // unhook this function to prevent indefinite loop
        remove_action('save_post', 'my_save_post');

        // update the post to change post status
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));

        }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:53:52+00:00Added an answer on June 12, 2026 at 3:53 pm

    Here you go:

    add_action( 'save_post', 'my_save_post', 10, 2 );
    function my_save_post( $post_id, $post ) {
        if ( wp_is_post_revision( $post_id ) )
            return;
    
        $matches = array();
        preg_match_all( '/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $matches );
    
        $first_link = false;
        if ( ! empty( $matches[1][0] ) )
            $first_link = $matches[1][0];
    
        update_post_meta( $post_id, 'link', esc_url_raw( $first_link ) );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add and remove some rows from a table using Jquery. But
I am trying to add my first unit test to an existing Open Source
I'm exploring Rails for the first time and trying to add some fairly straightforward
I'm trying to add a variable within a new variable. My first variable is:
I'm messing with backbone.js Todos for the first time. I'm trying to add a
I'm using the dynatree plugin for the first time, and I'm trying to add
I'm using (trying to use) jQuery to add a class to the first paragraph
Im new to asp.net mvc. I'm trying add new model class but it got
I have been trying to get up to speed on Named Pipes this week.
Hey all, my first post :D Problem: I'm trying to make a template gallery,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.