...

View Full Version : ★ NEWBIE ALERT ★ Writing to db in wordpress functions.php file



vestaviascott
11-02-2009, 11:47 PM
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?

tomws
11-03-2009, 04:00 AM
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.

vestaviascott
11-03-2009, 05:56 AM
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)

tomws
11-03-2009, 02:57 PM
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.

vestaviascott
11-03-2009, 03:14 PM
In the end it was so simple, I'm almost embarrassed to post the solution...


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

tomws
11-03-2009, 03:38 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum