How to Include Wordpress Functions in Custom .PHP File

How to include WordPress functions in custom .php file?

You're on the right track. Try this instead:

require_once("../../../../wp-load.php");

calling WordPress inbuilt functions in a custom php file

In any custom page you can call below three lines to pull up WordPress stack and then use any WordPress functionality

<?
//Imp to include
include('wp-load.php');
define('WP_USE_THEMES', false);
require('wp-blog-header.php');

// check is user is logged - if yes then print its role
if(is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
echo "role is ".$role[0];

}
?>

Can't use wordpress functions in custom php file

ABSPATH is defined in wp-config.php - you'd have to require that as well.

I haven't tried it, but this page may help. Though according to the comment on this answer, just including wp-load.php may do the trick.

Use Woocommerce functions in custom php files

In general on Wordpress/WooCommerce you will include your functions code:

  • In your active child theme (or active theme) function.php file
  • In a plugin…

You can also enable some code in:

  • Your theme templates
  • WooCommerce templates that you will override through your active child theme (or active theme).

Now to execute that function, you will need an event that will execute your function.

In (Wordpress) Woocommerce there is a lot of action hooks that are triggered on some specific events that you can use to execute your function. In this case your function will be hooked (ready to be executed on a specific event).

If you want to change the status of a specific order is better to do it in the related order edit page in backend.

An example:

For example you can change the order status when a customer has submit his order after checkout on order-received end point (thankyou page):

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) return;

// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Change order status to "completed"
$order->update_status( 'completed' );
}

This code is an official code snippet: Automatically Complete Orders.

It is a good example that shows you how things can work… So in your case you are using here WC_Order class methods like update_status().

Now with this code base, you can refine the behaviors like in this answer:

WooCommerce: Auto complete paid Orders (depending on Payment methods)


Related to orders: How to get WooCommerce order details

Can I Include php in functions.php? - Wordpress

If you want to include script and pass a parameter to him, you can create function in including script and use construction like this:

some script to include (file.php):

function some_function ($id) {
//some code, using $id
}

and calling file.php and pass a parameter:

include('file.php'); 
some_function(23);

Function include() simply adds the code from including file to the file where the function was originated
http://php.net/manual/en/function.include.php

How to acces wordpress functions in self-written php code?

Okay, I figured it out. I didn't use my local machine anymore but I used a webserver instead. There are no errors and I am not being redirected when I load 'wp-load.php'.

It is strange that almost the same code runs fine on a public webserver but not on a local machine. But it's working now so it's fine.

The code: (I used this and uploaded it to the WordPress root directory which contains the other files like wp-config.php etc.)

<?php
define( 'WP_USE_THEMES', false );
require( 'wp-load.php' );
$rootD = $_SERVER['DOCUMENT_ROOT'];
define("ABSPATH", "$rootD/httpdocs");
define("WPINC", "/wp-includes");

include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );

$client = new WP_HTTP_IXR_Client( 'http://mywordpresswebsite/xmlrpc.php' );

$client->query('wp.invoice', array(
$method = 'get_invoice',
$credentials = array('username', 'password'),
$args = array(
'ID' => 1032017039
)
));

$the_invoice = $client->getResponse();

foreach ($the_invoice as $key => $value) {
echo "$value";
}

?>

Thanks for all the help.

Add custom php file in child theme WP

@arcath is right you can not add php files using Enqueue functions. They are only used for adding/overwritting .css and .js files. That is also by two different methods for stylesheets you use wp_enqueue_style and for Javascript you use wp_enqueue_scripts.

Don't call method again and again best way to call enqueue method is by calling it only once in your function.php inside child directory example.

function adding_scripts_and_styles() {
wp_enqueue_script('unique_child_custom_js', get_stylesheet_directory_uri() . '/directory_path_if_any/custom.js', array('jquery'), true, true );
wp_enqueue_script('unique_child_custom_css', get_stylesheet_directory_uri() . '/directory_path_if_any/custom.css');
}

add_action( 'wp_enqueue_scripts', 'adding_scripts_and_styles');

For overwriding wordpresss templates create a php file in you child theme wordpress directory with the same name. Wordpress reads child theme template files first while loading.

Example if you want to overwride archive.php page template, create an archive.php in your child theme and then wordpress will use archive.php file from your child theme ignoring the parent theme archive.php.

Hope this help! Happy Coding :)



Related Topics



Leave a reply



Submit