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 thought I would find an answer to my question on the internet but I was not able to. This is not a big question, but as it is my first plugin, I want to make sure that everything is right before submitting.

What I am doing is adding a link to navigation menu depending on a value that admin selects from a settings panel in dashboard. I am accessing db in my custom javascript to add data to tables (in admin dashboard) and then fetching that data in another php script using "wp_nav_menu_items" filter.

I used

wp_localize_script( 'my-ajax-request', 'accessDB', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

to get hold of admin-ajax.php path. I then send my ajax calls to url referred by accessDB.ajaxurl. This part is working fine. And I think this is the right way to make ajax calls in Wordpress plugin. If not please correct me.

The second part is where I have to get the value from db. This is the value I need in a wordpress filter hook wp_nav_menu_items. And this is how it goes:

function add_custom_menu_link($items, $args) {

    global $wpdb;
    $row = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "custom_table");

    if (count($row) > 0) {
        return $items . "<li class='menu-item'><a href='" . $row->url . "' target='_blank'>Custom Link</li>";
    } else {
        return $items;
    }
}

add_filter('wp_nav_menu_items', 'add_custom_menu_link', 10, 2);

So my question is that, am I doing it the right way, or do I have to use cURL to get the data by using admin-ajax.php? Or is there any other way that I am missing?

Regards

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I don't understand if it works for you or not.

For me the code is not wrong, maybe can be optimized.

For the ajax request, if it is made in the backend, yo do not need to pass the ajax url using wp_localize_script. From WP 2.8 a global variable ajaxurl is defined to be used in ajax calls in the admin area. So you can delete the wp_localize_script and use directly ajaxurl in the javascript.

Second optimization in the db request. Now it works, (or at least it should for me) but if you need only the variable $row->url you can use $wpdb->get_var, like so:

$url = $wpdb->get_var("SELECT url FROM " . $wpdb->prefix . "custom_table");
if ( filter_var($url, FILTER_VALIDATE_URL) ) {
    return $items . "<li class='menu-item'><a href='" . esc_url($url) . "' target='_blank'>Custom Link</li>";
} else {
   return $items;
}

What is strange here is the missing of a 'WHERE' clause in your SQL. Seems you save only one value in a custom table. If so, is much better using the standard options table and using get_option / update_option to get and save the values.

share|improve this answer
    
Thanks for the answer. I did not know that Wordpress had a global variable for ajax calls. –  Jehanzeb.Malik Aug 21 '13 at 10:15
    
@Jehanzeb.Malik. You're welcome. Remember that it is only in admin area. In frontent you still pass the url like you do in posted code. –  G. M. Aug 21 '13 at 10:18
    
Does Wordpress allow to add a custom variable to options table? I mean can I store a variable like update_option('my_custom_message', 'Hello World')? –  Jehanzeb.Malik Aug 21 '13 at 11:01
1  
Of course. you can also use update_option('my_custom_message', array('Hello', 'World')) see here and here @Jehanzeb.Malik –  G. M. Aug 21 '13 at 11:17
    
Just another small question. I also want to include another php file in my main plugin file. Do I simply use include_once at the top of file. Or is there action hook that I should use? –  Jehanzeb.Malik Aug 21 '13 at 11:52

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.