Support » Fixing WordPress » 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 = '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 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”

Viewing 13 replies - 1 through 13 (of 13 total)
  • Try this:

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

    (@jole5)

    Could you please provide me with more help, explanation?

    I added your code (without any modification) to my single.php, but nothing happens. I tried to open some post pages like this:

    www.my-site.com/category/post-name?some text here

    but that function does not create “my_new_custom_field_name” custom field at all.

    Am I doing something wrong?

    Please put your single.php file in a pastebin and post a link to it here.

    Thread Starter Advanced SEO

    (@jole5)

    I am testing it using Twenty_Fourteen single.php template.

    Here is code on pastebin

    The code I gave you needs to go inside the Loop, just after this line: while ( have_posts() ) : the_post();

    Thread Starter Advanced SEO

    (@jole5)

    Firstly I tried inside loop. Again I tried like you said inside loop.
    You can look at new code here.

    But it is not working, nothing happens, it does not crate any custom field.

    Could you please look at it again, and update?

    You need to set $currenturl and $id. Try this inside the loop:

    $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);
    }
    Thread Starter Advanced SEO

    (@jole5)

    Thank you that is exactly what i need.

    Is there way to strip out something that I do not wish to add to custom field?

    For example if this URL is opened:
    www.site.com/category/subcategory/post-name?some-text-here.html

    With your code, new custom field will be with value “some-text-here.html”.
    Is there way to remove “.html” to add just “some-text-here” as custom field value?

    Thanks, again.

    Is it always .html that you want to remove?

    Thread Starter Advanced SEO

    (@jole5)

    URLs will be like this:

    www.site.com/category/subcategory/post-name?-some-text-here.html
    www.site.com/category/subcategory/post-name?-some-other-text-here.html

    So, always it will have “-” just after question sign (?) and “.html” at the end. It would be great to remove first “-” and “.html”.

    From above example new custom field should be with value like this:
    some-text-here
    or
    some-other-text-here

    Thread Starter Advanced SEO

    (@jole5)

    I believe I could remove first “-” just after question sign by modifying preg_match like this:

    if ( preg_match('/\?-(.+)$/', $currenturl, $matches) )

    I just added “-” after “?”, it should give this:
    some-text-here.html
    or
    some-other-text-here.html

    How to remove “.html”?

    Try this:

    if ( preg_match('/\?-(.+)\.html$/', $currenturl, $matches) ) {
    Thread Starter Advanced SEO

    (@jole5)

    Thank you, it works just fine.

    —–
    If I ever need to use this code to add custom field but to replace something in URL with something else, for example “.html” with “something else”, is it possible, and how to do it?

    I am closing this question, as resolved, thanks to your great help.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Add custom field with some part of URL as value’ is closed to new replies.