0

I wish to move my site from some other CMS to WP. There is no any usefull migration, export option in my current CMS so I will need to create posts again manually. I will to transfer important fields of posts on current CMS to custom fields on WP. I am thinking to create some php functions wich will add some custom field to post with some data on new WP site.

Like I said there is no any useful export tool on my current CMS (mostly file system), but I can make on my current CMS post link with some added variable like this:

www.my-site.com/category/subcategory/post-name?value-of-some-field

Then I will crawl my own site to find and make list of links with that suffix "?value-of-some-field". After that I will update links list to new site location on WP test site like this.

www.test-wp.my-site.com/category/subcategory/post-name?value-of-some-field

Then I would like to use code on single.php page to check if url contain "?value-of-some-field" and if it is true to make some custom field with data "value-of-some-fields".

I managed to do this successfully when I know which value I can expect, in case there is several, limited number of different values like this:

<?php
$currenturl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

// Look at URL
$pos1 = strpos($currenturl, "?value-one");
$pos2 = strpos($currenturl, "?value-two");
//...

// If URL has "?value-one" create new custom field and add value-one
if($pos1 == true) {
add_post_meta($id, 'my_new_custom_field_name', 'value-one', true);
}
// If URL has "?value-two" create new custom field and add value-two
 elseif($pos2 == true) {
add_post_meta($id, 'my_new_custom_field_name', 'value-two', true);  
}
// ...
?>

What I need is code wich will create new custom field and add data inside it copied from url after "?" sign.

For example if url is like this:

www.site.com/category/subcategory/post-name?some textual description will be here

program will crawl and save that link like this:

www.site.com/category/subcategory/post-name?some%20textual%20description%20will%20be%20here

Of course I can make some START, END markers in url if needed like this

www.site.com/category/subcategory/post-name?START__some%20textual%20description%20will%20be%20here__END

What I need is code which will check if url contains something between "START__" and "__END" and to create new custom field with text between "START__" and "__END", and if it is possible to replace "%20" with space " ".

In this example to create new custom field with value "some textual description will be here"

1 Answer 1

0

I found this solution, it works great. I will share it here if someone else need it.

$currenturl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$id = $post->ID;

if ( preg_match('/\?(.+)$/', $currenturl, $matches) ) {
   $value = urldecode($matches[1]);
   add_post_meta($id, 'my_new_custom_field_name', $value, true);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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