728x90

I have problems in pointing to the database in Wordpress. I have tried to include global $wpdb and it does not work. And I have also done PHP include for the wp-load.phpwp-db.php, wp-config.php but still no fix.

It says,

Fatal error: Call to a member function get_results() on a non-object in C:\xampp\htdocs\wordpress\wp-content\themes\cv_test\searchresult_details.php on line 47

Sorry, I'm a beginner in Wordpress development. Any help is appreciated. Thanks.

    include_once('http://localhost/wordpress/wp-config.php');
include_once('http://localhost/wordpress/wp-load.php');
include_once('http://localhost/wordpress/wp-includes/wp-db.php');

function retrieveClientDesc()
{
    global $wpdb;

    $query = "SELECT client_desc FROM wp_client WHERE client_name =  'Cal'";
    $result = $wpdb->get_results($query, OBJECT);



    for($i = 0; $i<=count($result); $i++)
    {
        $clientDesc = ($result[$i]->client_desc);
        echo $clientDesc;
    }

    print_r($result);
}

This is part of the codes. It keeps says my $result section has a fatal error.

    I have had this problem once and this is how i was able to solve it.

    global $wpdb, $table_prefix;
    
    if(!isset($wpdb))
    {
        //the '../' is the number of folders to go up from the current file to the root-map.
        require_once('../../wp-config.php');
        require_once('../../wp-includes/wp-db.php');
    }


      728x90

      There is a specific way for adding Javascript to your WordPress theme in order to prevent any conflicts with plugins or parent WordPress Themes. The problem is that many “developers” call their javascript files directly in the header.php or footer.php file which is the incorrect way of doing it.

      In this guide I will show you how to correctly call your javascript files using your functions.php file so they load right into your site’s head or footer tag. This way if you are developing a theme for distribution and your end user wants to modify the scripts via a child theme they can or if they are using minifying/caching or other optimization plugins they will work correctly. And if you are working with a child theme you  your scripts are added the right way, without copying the header.php or footer.php files into your child theme which are shouldn’t ever be necessary (when working with a well coded theme)

      Wrong Way To Add Javascript To WordPress Themes

      Calling the javascript in your header.php file or footer.php file such as shown below, is NOT the correct way and I highly recommend against it, often times it causes conflicts with other plugins and doing things manually when working with a CMS is never a good idea.

      <script src="https://site.com/wp-content/themes/mytheme/js/my-script.js"></script>

      The Right Way To Add Javascript To WordPress Themes

      The better for adding javascript to your WordPress theme is to do so via the functions.php file using wp_enqueue_script. Using the wp_enqueue_scripts action to load your javascript will help keep your theme out of trouble.

      Example

      wp_enqueue_script( 'my-script', get_template_directory_uri() . '/js/my-script.js', array(), true );

      The code above will load the my-script.js file on your site. As you can see I have only included the $handle but you can also add dependencies for your script, version number and whether to load it in the header or footer (default is header).

      The wp_enqueue_script() function can technically be used in any theme or plugin template file but if you are loading global scripts you’ll want to place it either in your theme’s function.php file or in a separate file specifically intended to load scripts on the site. But if you are only looking to load a script on a specific template file (for example on gallery posts) you could place the function right in the template file, however, personally I recommend keeping all the scripts in one place and using conditionals to load scripts as needed.

      WordPress Hosted Scripts

      One cool thing about WordPress is there are already a bunch of scripts hosted by and registered that you can use in your theme development. For example jQuery which is used in almost every project should ALWAYS be loaded from WordPress and never hosted on a 3rd party site such as the Google. So before you add a custom script to your project check the list of registered scripts to make sure it’s not already include in WordPress and if it is you should load that one as opposed to registering your own.

      Using The WordPress Enqueue Hook

      Previously we mentioned the function needed to load a script on your site, however, when working with non template files such as your functions.php file you should be adding this function inside another function hooked into the proper WordPress hooks, this way your scripts get registered with all the other scripts registered by WordPress, 3rd party plugins and your parent theme when using a child theme.

      WordPress has two different action hooks you can use for calling your scripts.

      1. wp_enqueue_scripts – action used to load scripts on the front-end
      2. admin_enqueue_scripts – action used to load scripts in the WP admin

      Here is an example (that would be added to your functions.php file) of how to create a function and then use the WordPress hook to call your scripts.

      /**
       * Enqueue a script
       */
      function myprefix_enqueue_scripts() {
          wp_enqueue_script( 'my-script', get_template_directory_uri() . '/js/my-script.js', array(), true );
      }
      add_action( 'wp_enqueue_scripts', 'myprefix_enqueue_scripts' );

      Note: See how we are using the “get_template_directory_uri” function when defining the location of your script? This function creates a URL to your theme folder. If you are working with a child theme you will want to use “get_stylesheet_directory_uri” instead so that it points to your child theme and not the parent theme.

      Adding Inline Javascript Code

      While you can easily paste inline javascript in any template file via the script tag it can be a good idea to also use WordPress hooks for adding your inline code, especially when it’s a core plugin or theme code. Below is an example of adding inline scripts to your site:

      function myprefix_add_inline_script() {
          wp_add_inline_script( 'my-script', 'alert("hello world");', 'after' );
      }
      add_action( 'wp_enqueue_scripts', 'myprefix_add_inline_script' );

      What this will do is add your inline javascript after the previously registered “my-script” script. When using wp_add_inline_script you can only add inline code either before or after an already registered script so if you are trying to modify the code of a particular script make sure it’s loaded after it, or if you just need to add some custom code you can hook it to the jquery script which is generally loaded by most WordPress themes and if not you can use the wp_enqueue_script to load the WordPress hosted version of jQuery.

      By using this method people can easily remove your inline scripts via a child theme or plugin add-on, it keeps all your custom javascript neatly organized and it is parsed by WordPress which can be safer. And if you are using a child theme you can load your scripts via your functions.php file instead of copying over the header.php or footer.php files over to your child theme.

      That said, if you are working in a child theme you don’t need to do this you can simply dump your code into your header either using the wp_head or the wp_footer hooks such as the example below:

      add_action( 'wp_footer', function() { ?>
      	<script>
      		( function( $ ) {
      			'use strict';
      			$( document ).on( 'ready', function() {
      				// Your custom code here
      			} );
      		} ( jQuery ) );
      	</script>
      <?php } );
      • Updated on: 
      • Posted Under: Tutorials


      728x90

      When updating to WooCommerce 2.1.1. I get this notice:

      Notice: Woocommerce->add_inline_js is deprecated since version 2.1! Use wc_enqueue_js instead. in /nas/wp/www/staging/webwinkels001/wp-includes/functions.php on line 2908
      

      For some reason, this is also conflicting with my contact page you can find here:

      http://webwinkels001.staging.wpengine.com/contact/
      

      When you press 'Verzenden' normally it should show only errors you have to fill in the fields, but now I get this:

      Notice: Woocommerce->add_inline_js is niet meer in gebruik sinds versie2.1! Gebruik in plaats daarvan wc_enqueue_js. in /nas/wp/www/staging/webwinkels001/wp-includes/functions.php on line 2908
      {"90":"Dit veld mag niet leeg blijven.","92":"Dit veld mag niet leeg blijven.","95":"Dit veld mag niet leeg blijven."}
      

      When I'm LOGGED OUT I also see this on WooCommerce pages:

      Notice: Woocommerce->setup_product_data is niet meer in gebruik sinds versie2.1! Gebruik in plaats daarvan wc_setup_product_data. in /nas/wp/www/staging/webwinkels001/wp-includes/functions.php on line 2908
      
      @coenjacobs
      Contributor

      coenjacobs commented on 13 Feb 2014

      I think the error shows pretty clear what the issue is. You (or your theme) shouldn't use the add_inline_jsmethod anymore, but use the wc_enqueue_js function instead.

      This is not a support channel though, as this is not a corre issue. You can post this kind of questions in our public support forum: http://wordpress.org/support/plugin/woocommerce or in case you are a WooThemes customer via our support portal: http://support.woothemes.com

      @coenjacobs coenjacobs closed this on 13 Feb 2014

      @Willem-Siebe

      Hi Coen,

      I thought this is a WooCommerce issue, so I posted it here.
      I don't know how you see it's caused by my theme, I think I still have to learn a lot.
      But know I know it's my theme since you said it is, I will contact them.

      Thanks.

      @coenjacobs
      Contributor

      coenjacobs commented on 13 Feb 2014

      I'm not sure it's your theme, it can also be a plugin. But it's not WooCommerce core, since we've taken all usage of that function out of it, which is why I recommend you use a support channel.

      Please note that this is a notice only. If you disable debug mode on your site, the notices (and possible warnings too) will be suppressed and will not be shown. This doesn't mean you shouldn't fix it, or ask for a fixed version of your theme, as this will start to cause issues in future versions.


      728x90


      I have a basic html file which is attached to a java program. This java program updates the contents of part of the HTML file whenever the page is refreshed. I want to refresh only that part of the page after each interval of time. I can place the part I would like to refresh in a div, but I am not sure how to refresh only the contents of the div. Any help would be appreciated. Thank you.

      Use Ajax for this.

      Build a function that will fetch the current page via ajax, but not the whole page, just the div in question from the server. The data will then (again via jQuery) be put inside the same div in question and replace old content with new one.

      Relevant function:

      http://api.jquery.com/load/

      e.g.

      $('#thisdiv').load(document.URL +  ' #thisdiv');

      Note, load automatically replaces content. Be sure to include a space before the id selector.


        + Recent posts