WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Correct metgod to include wp-load.php (12 posts)

  1. wdlyons
    Member
    Posted 1 year ago #

    Hi,

    I submitted the plugon and received the following email back from WordPress:

    Including wp-config.php, wp-blog-header.php, wp-load.php, or pretty much any other WordPress core file that you have to call directly via an include is not a good idea and we cannot approve a plugin that does so unless it has a very good reason to load the file(s). It is prone to failure since not all WordPress installs have the exact same file structure.

    Usually plugins will include wp-config.php or wp-load.php in order to gain access to core WordPress functions, but there are much better ways to do this.

    It's best if you tie your processing functions (the ones that need but don't have access to core functions) into an action hook, such as "init" or "admin_init".

    Please consult the Plugins API reference for more information: http://codex.wordpress.org/Plugin_API

    If you're trying to use AJAX, please read this: http://codex.wordpress.org/AJAX_in_Plugins

    When you've corrected your code, reply to this email with the updated code attached, or provide a link to the new code.

    However, I do not use include and the only file I call is wp-load.php

    The code I use to do this is:

    $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
    require_once( $parse_uri[0] . 'wp-load.php' );

    Can anyone point me in the right direction to what I need to be using if this is not accepted.

    I call that file because withput it I get a

    Fatal error: Call to undefined function error

    Thanks

    Warwick

  2. Chris
    Member
    Posted 1 year ago #

    1. Instead of parsing $_SERVER you could simply go for require_once ABSPATH . 'wp-load.php';

    2. include() and require() are only different in terms of the consequences when a file does not exist. Other than that they are kind of interchangabel.

    3. The question is: What is it that you want to do after you've loaded the wp-load.php? Why would you need to include that file? What function do you call that is causing the error?

  3. wdlyons
    Member
    Posted 1 year ago #

    Thanks for your reply Chris,

    After I load the wp-load.php file I grab form data using POST and run them through the following functions:

    sanitize_text_field
    check_input

    The code:

    <?php
    
    $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
    require_once( $parse_uri[0] . 'wp-load.php' );
    
     // Get values from form
    
    $person = sanitize_text_field( $_POST['person'] );
    $person = check_input( $_POST['person']);
    
    $person_id = sanitize_text_field( $_POST['person_id'] );
    $person_id= check_input($_POST['person_id'], "You need to Choose The 1st Person");
    
    $spouse_id = sanitize_text_field( $_POST['spouse_id'] );
    $spouse_id= check_input($_POST['spouse_id'], "You need to Choose The 2nd Person");
    
    $mday = sanitize_text_field( $_POST['mday'] );
    $mday = check_input( $mday);
    
    $mmonth = sanitize_text_field( $_POST['mmonth'] );
    $mmonth = check_input( $mmonth);
    
    $myear = sanitize_text_field( $_POST['myear'] );
    $myear = check_input( $myear);
    
    $date_of_marriage = ($mday." ".$mmonth." ".$myear);
    $date_of_marriage = sanitize_text_field( $date_of_marriage );
    $date_of_marriage = check_input( $date_of_marriage);
    
    // Insert the values above into the person table
    
    include ('tablename.php');
    
    //	$family_id = $wpdb->get_var( "SELECT family_id FROM $table_name WHERE id = $id" );
    
    $wpdb->insert($table_name2,array('person_id'=>$person_id, 'spouse_id'=>$spouse_id, 'date_of_marriage'=>$date_of_marriage));
    
    $strSql = 'select last_insert_id() as lastId';  
    
      $result = mysql_query($strSql);  
    
      while($row = @mysql_fetch_assoc($result)){  
    
              $marriage_id = $row['lastId'];
    
      }
     ?> 
    
    <?php
    
    $wpdb->update($table_name2,
    	array('marriage_id'=>check_input($marriage_id)),
    	array('id'=>check_input($marriage_id)));
    
    //Once the page creation has been completed forward to the view person list
    
    header("Location: ".bloginfo('url')."/wp-admin/admin.php?page=add-submenu-view-spouse");
    
    ?>
  4. Chris
    Member
    Posted 1 year ago #

    sanitize_text_field() is a WordPress core function (located in /wp-includes/formatting.php) and therefore should always be ready to use - without the need of an additional loading of wp-load.php.

    check_input() on the other hand seems to be a proprietary function. If it's not declared in your plugin code you should not rely on that - since you can never be sure other users will have that, too - but write your own function with a proper namespacin prefix for it.

  5. wdlyons
    Member
    Posted 1 year ago #

    Hi Chris,

    Thats what I thought too but without the wp-load.php sanitize_text_field()comes up with a Fatal error: Call to a member function error.

    I have the check_input function declared in my main plugin file

  6. Chris
    Member
    Posted 1 year ago #

    Ah okay.

    Have you checkend var_dump( get_included_files() );exit;
    to see if /wp-includes/formatting.php appears?

    And when you say "Fatal error: Call to a member function error", are you sure it's about sanitize_text_field() cause this is function and not a method. Maybe it's about the database object? Have you tested using global $wpdb; before using it?

    Other than that I'm out of ideas at the moment, sry

  7. bcworkz
    Member
    Posted 1 year ago #

    It's silly to do things this way IMO, but you could POST to a WordPress page, then the WordPress environment is already loaded for you. Your form action could possibly be to the page your code is already forwarding to. Let the page template process the form data before displaying what ever it normally does.

  8. wdlyons
    Member
    Posted 1 year ago #

    Thanks Chris, bcworkz

    I will try some of what you suggest and see how it goes.

    I'll post how it goes over the next day or two.

    Appreciate the assistance

    cheers

    Warwick

  9. wdlyons
    Member
    Posted 1 year ago #

    Sorry to take so long to get back.

    I finally got around the problem by posting the affected form info to itself and running the corresponding code on the same page.

    Thanks for all your help

    Warwick

  10. twanny
    Member
    Posted 8 months ago #

    Hi wdlyons,

    I am pleased for you for solving your problem but what was the actual solution? What did you actually do to solve your problem. I am not a php / WP guru so unless things are explained they dont make sense. I will be interested to see how you solved your problem.

    regards

  11. esmi
    Forum Moderator
    Posted 8 months ago #

    @twanny: If you require assistance then, as per the Forum Welcome, please post your own topic.

  12. bcworkz
    Member
    Posted 7 months ago #

    I have more information to share regarding the OP's problem of accessing WordPress functions from external files. The plugin review team's standard message regarding this suggests using AJAX. Such a solution is almost always possible, though not always desirable. As it happens, there is a similar technique to the AJAX approach, except no javascript or jQuery is required, your request handler can be initiated from a simple HTML link or form action. It is suitable for any GET or POST request. The handler can include another PHP file and in the process enable access to WP functions on that file.

    See Plugin_API/Action_Reference/admin_post_(action).

    Better late than never :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags