Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ★ NEWBIE ALERT ★ Writing to db in wordpress functions.php file

    Hi, I'm new here so please take it easy on me with this first post :-)

    What I need to do is to create a script that will reside in my theme's functions.php file that either automatically executes when the file is loaded or on a button click and updates the db as follows...

    update wp_options set uploads_use_yearmonth_folders = 0;
    update wp_options set permalink_structure = '/%postname%/';

    Can someone help me with the script?

    In classic asp, I would use something like this...

    sql = update wp_options set uploads_use_yearmonth_folders = 0
    sql2 = update wp_options set permalink_structure = '/%postname%/'
    set rs = conn.execute(sql)
    set rs2 = conn.execute(sql2)
    rs.close
    rs2.close

    where "conn" is the open db connection

    What is the PHP equivalent?

    I'm working in wordpress 2.8.5 (also, do I have a connection object when in functions.php or do I need to create one?
    Last edited by vestaviascott; 11-03-2009 at 12:20 AM.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You should probably check some Wordpress-specific forums for help on this as it seems to require inside information on how Wordpress works - beyond what is easily available in API docs, anyway.
    Are you a Help Vampire?

  • #3
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    You should probably check some Wordpress-specific forums for help on this as it seems to require inside information on how Wordpress works - beyond what is easily available in API docs, anyway.
    Things are slow over there. I was hoping to get a quick answer here. Its just an MySQL database and I know the tables and values that need to be updated. How difficult can it be to execute an update statement on the db?

    here's a better explanation of what I'm trying to do...

    I've developed a theme system and I want it to do a double duty of setting up some default option values in the database.

    So here's what I'm trying to do...

    When the template is installed, I want the installer to also...

    1) Set permalinks to /%postname%/
    (I'm assuming its a simple update statement on the wp_options table)

    2) Set "uploads_use_yearmonth_folders" to 0 (the default is true, I want it to be false)

    3) Set privacy options (blog_public) to 1 (the default is false)

    Any help much appreciated. Seems simple enough to update a few fields in the db, but I'm not sure how to create a connection and also if I can intercept the template build process to make these edits programmatically or if it will need to be a manual button click on the template's options screen (or perhaps even via a plug in)

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by vestaviascott View Post
    When the template is installed, I want the installer to also...
    That's the part that requires Wordpress-specific knowledge. The PHP/MySQL part is trivial and is widely available via the site search here or Google. If the Wordpress integration part is "simple", then you shouldn't have a problem making the standard PHP/MySQL tutorial code work for you.
    Are you a Help Vampire?

  • #5
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In the end it was so simple, I'm almost embarrassed to post the solution...

    Code:
    <?php update_option( $option_name, $newvalue ); ?>

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That's the beauty of a robust CMS. What you want to make more complex is already simplified. That's exactly how Drupal handles it, only they're called variables instead of options.
    Are you a Help Vampire?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •