Add custom field with some part of URL as value
-
I would like to use code on single.php page to check if url contain “?some text here” and if it is true to make new custom field with data “some text here”.
I managed to do this successfully when I know which value I can expect, in case there is several different values like this:
<?php $currenturl = 'https://' . $_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 which 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
in browser it will be 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”
- The topic ‘Add custom field with some part of URL as value’ is closed to new replies.