0

I have the following code in my functions.php

$ar = array(
    'price_content' => 'hello'
    );

$result = $wpdb->insert('hs_prices', $ar); // insert 'hello' into 'price_content'

This should just create one row, but it creates 12 in the database. I am on the homepage of the website when I run this (by refreshing the page).

Could this be because the code is in functions.php, and it's being included 12 or so times throughout the site?

edit: It shouldn't be because it's in functions.php, as it also does it when on index.php. Must be the query.

What could be the cause of this?

2
  • Did you add the code snippet into the global scope of functions.php or inside some action/filter ?
    – birgire
    Jun 3, 2014 at 10:07
  • Global scope. Should it be linked with a filter?
    – tmyie
    Jun 3, 2014 at 10:42

1 Answer 1

0

This works if add this code in functions.php?

if ( ! function_exists( 'test_setup' ) ) :

function test_setup() {

    $ar = array(
    'price_content' => 'hello'
    );

$result = $wpdb->insert('hs_prices', $ar); // insert 'hello' into 'price_content'

}

add_action( 'after_setup_theme', 'test_setup' );
1
  • Please add a proper explanation to how your code works and why it should work. Don't just dump code and says it works. Jun 3, 2014 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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