在Wordpress中自动生成自定义字段值

在Wordpress中自动生成自定义字段值

问题描述:

I run a few side blogs that I sort of aggregate into my main blog. I use simplepie to parse the feeds from my other blogs, so the posts are being created automatically.

My typical post is layed out like this:

  1. IMAGE
  2. CONTENT/TEXT
  3. HYPERLINK

What I'm looking to do is automatically grab the hyperlink, and insert it into a Custom Field. The custom field already exists in the post, but I need to insert the hyperlink contained in the post content as the value.

I would need just the link, without the html, so the value would be just a straight link - http://domain.com/fsdds

I know there are a number of plugins that accomplish this with images, but I haven't seen anything that will do it with anything else, like hyperlinks.

I posted this question over at the Wordpress forums and was told I'd have to parse the entire post content looking for the links, which I knew, the problem is I'm not too sure how to do that.

Thanks

我运行一些副博客,我将这些博客汇总到我的主博客中。 我使用simplepie来解析feed 来自我的其他博客,所以帖子是自动创建的。 p>

我的典型帖子是这样的: p>

  1. IMAGE li>
  2. 内容/文字 li>
  3. HYPERLINK li> ol>

    我要做的是自动抓住 超链接,并将其插入自定义字段。 自定义字段已存在于帖子中,但我需要将帖子内容中包含的超链接作为值插入。 p>

    我只需要链接,不需要html,所以值 只是一个直接的链接 - http://domain.com/fsdds p>

    我知道有很多插件可以通过图像实现这一点,但我还没有看到任何其他内容,比如超链接。 p>

    我发布了这个问题 在Wordpress论坛上被告知我必须解析整个帖子内容寻找链接,我知道,问题是我不太清楚如何做到这一点。 p> 谢谢 p> div>


Building on Anthony's answer, use the UPDATE POST META once you have your link...

Put this in your functions.php file:

function catch_that_link() {
      global $post, $posts;
      $first_link = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/(https?://)?(www.)?([a-zA-Z0-9_%]*)\b.[a-z]{2,4}(.[a-z]{2})?((/[a-zA-Z0-9_%])+)?(.[a-z])?/', $post->post_content, $matches);
      $first_link = $matches [1] [0];

      if(empty($first_link)){ //Defines a default image
        return 'no link found';
      }
      return $first_link;
    }

Then in your query loop, category file or whatever php file you would do the following

<?php 

$post_id = 13; //replace the number with the specific post
$meta_key = 'key_example' //replace with your custom field name
$meta_value = catch_that_link();

update_post_meta($post_id, $meta_key, $meta_value); 
?> 

I've been thinking about this myself and the solution has to be to run a function on the save_post action hook. Unfortunately this is undocumented in the Wordpress Codex and I haven't had time to look further afield yet.

Just stumbled upon this Function Reference/add post meta

Still need a way to grab the hyperlink from the post and and insert it as the $metavalue though.

This is the function that grabs the first image in a post:

function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_img = $matches [1] [0];

      if(empty($first_img)){ //Defines a default image
        $first_img = "/images/default.jpg";
      }
      return $first_img;
    }

you would just need to replace the first preg_match_all parameter to:

'/(https?://)?(www.)?([a-zA-Z0-9_%])\b.[a-z]{2,4}(.[a-z]{2})?((/[a-zA-Z0-9_%])+)?(.[a-z]*)?/'

Add the whole function to your functions.php, and call that function from your script. It should return the first link it finds in the post content.