Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to store simple data, a few links inside wp_options. Basically using the following way: update_option( 'simple_links', '<a href="">link 1</a>' );

my question is if it is allowed, I dont want to create new table for it, just a few links permanently stored inside the footer. Is that the valid way and will it actually save data inside options? Or there is any other better way for such a simple need?

Thank you.

share|improve this question
    
Yes, that's a perfectly valid way of storing some options in the database, however, it's good practice to prepend your option name with some unique characters pertaining to your site or something, like 'my_simple_links' to avoid possible collisions. Also, if you're going to have multiple links you can store an array inside one option by passing the array as the second argument (serialization will be performed automatically). Hope this helps. –  soulseekah Oct 18 '11 at 9:30
    
@Soulseekah Thank you so much! But how do I print the array then. I am trying that way but it just returns the word 'Array' no values: add_option( 'my_simple_links', $slink_array_new, '', 'yes' ); –  Alex Oct 18 '11 at 9:54
    
foreach ($links as $link) echo $link; –  soulseekah Oct 18 '11 at 9:55
    
@Soulseekah what i meant is that then inside footer I call it like that <?php echo get_option('my_simple_links'); ?> –  Alex Oct 18 '11 at 9:58
1  
arrays $array_var_name['value'] and objects: $object_var_name->value. Php.net is a good ressource for further research on that topic. Also: Don't forget to use esc_attr() on your values before dumping it into the DB. –  kaiser Oct 18 '11 at 10:22
show 3 more comments

1 Answer

up vote 0 down vote accepted

To recap the comment chain above:

I think that's a perfectly valid way of storing some options in the database, however, it's good practice to prepend your option name with some unique characters pertaining to your site or something, like 'my_simple_links' to avoid possible collisions with other plugins and themes that add_options.

Also, if you're going to have multiple links they could be stored as an array inside one option by passing the array as the second argument (serialization will be performed automatically).

Accessing them from your theme would be as easy as:

<?php
    $my_simple_links = get_option( 'my_simple_links' );
    foreach ($my_simple_links  as $link )
        echo $link;
?>

Better yet store them in an associative array as title => url and do this:

<?php
    $my_simple_links = get_option( 'my_simple_links' );
    foreach ( $my_simple_links as $title => $url )
        echo '<a href="'.$url.'">'.$title.'</a>';
?>

And don't forget to read the Codex on:

share|improve this answer
1  
You shouldn't use get_option() in the loop. Calling the function every time is a overhead. You should store the value in a variable first and then use that variable in the loop. –  Tareq Oct 18 '11 at 10:36
    
Great point, thanks for pointing it out. –  soulseekah Oct 18 '11 at 10:42
    
@Tareq it is not called in the loop. It is called once to form array to iterate. –  Rarst Oct 18 '11 at 10:42
    
@Rarst, although some PHP versions do go ahead and optimize this (my PHP version optimizes it to the point of calling the function twice only), I wouldn't depend on the implementation, and it has always been a best practice to keep evaluations out of loops. –  soulseekah Oct 18 '11 at 10:44
    
@Soulseekah hm, really? From quick test function gets only called once for me (PHP 5.3.8) and I considered no-brainer that it would work this way. I do not argue separate variable is not worth doing, but I'd think policing this for overhead that isn't there is too much. :) –  Rarst Oct 18 '11 at 10:47
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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