728x90
<?php
/**
* Plugin Name: WooCommerce Offline Gateway
* Plugin URI: https://www.skyverge.com/?p=3343
* Description: Clones the "Cheque" gateway to create another manual / offline payment method; can be used for testing as well.
* Author: SkyVerge
* Author URI: http://www.skyverge.com/
* Version: 1.0.2
* Text Domain: wc-gateway-offline
* Domain Path: /i18n/languages/
*
* Copyright: (c) 2015-2016 SkyVerge, Inc. (info@skyverge.com) and WooCommerce
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package WC-Gateway-Offline
* @author SkyVerge
* @category Admin
* @copyright Copyright (c) 2015-2016, SkyVerge, Inc. and WooCommerce
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*
* This offline gateway forks the WooCommerce core "Cheque" payment gateway to create another offline payment method.
*/
defined( 'ABSPATH' ) or exit;
// Make sure WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
return;
}
/**
* Add the gateway to WC Available Gateways
*
* @since 1.0.0
* @param array $gateways all available WC gateways
* @return array $gateways all WC gateways + offline gateway
*/
function wc_offline_add_to_gateways( $gateways ) {
$gateways[] = 'WC_Gateway_Offline';
return $gateways;
}
add_filter( 'woocommerce_payment_gateways', 'wc_offline_add_to_gateways' );
/**
* Adds plugin page links
*
* @since 1.0.0
* @param array $links all plugin links
* @return array $links all plugin links + our custom links (i.e., "Settings")
*/
function wc_offline_gateway_plugin_links( $links ) {
$plugin_links = array(
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=offline_gateway' ) . '">' . __( 'Configure', 'wc-gateway-offline' ) . '</a>'
);
return array_merge( $plugin_links, $links );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wc_offline_gateway_plugin_links' );
/**
* Offline Payment Gateway
*
* Provides an Offline Payment Gateway; mainly for testing purposes.
* We load it later to ensure WC is loaded first since we're extending it.
*
* @class WC_Gateway_Offline
* @extends WC_Payment_Gateway
* @version 1.0.0
* @package WooCommerce/Classes/Payment
* @author SkyVerge
*/
add_action( 'plugins_loaded', 'wc_offline_gateway_init', 11 );
function wc_offline_gateway_init() {
class WC_Gateway_Offline extends WC_Payment_Gateway {
/**
* Constructor for the gateway.
*/
public function __construct() {
$this->id = 'offline_gateway';
$this->icon = apply_filters('woocommerce_offline_icon', '');
$this->has_fields = false;
$this->method_title = __( 'Offline', 'wc-gateway-offline' );
$this->method_description = __( 'Allows offline payments. Very handy if you use your cheque gateway for another payment method, and can help with testing. Orders are marked as "on-hold" when received.', 'wc-gateway-offline' );
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions', $this->description );
// Actions
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
// Customer Emails
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
}
/**
* Initialize Gateway Settings Form Fields
*/
public function init_form_fields() {
$this->form_fields = apply_filters( 'wc_offline_form_fields', array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'wc-gateway-offline' ),
'type' => 'checkbox',
'label' => __( 'Enable Offline Payment', 'wc-gateway-offline' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'wc-gateway-offline' ),
'type' => 'text',
'description' => __( 'This controls the title for the payment method the customer sees during checkout.', 'wc-gateway-offline' ),
'default' => __( 'Offline Payment', 'wc-gateway-offline' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'wc-gateway-offline' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'wc-gateway-offline' ),
'default' => __( 'Please remit payment to Store Name upon pickup or delivery.', 'wc-gateway-offline' ),
'desc_tip' => true,
),
'instructions' => array(
'title' => __( 'Instructions', 'wc-gateway-offline' ),
'type' => 'textarea',
'description' => __( 'Instructions that will be added to the thank you page and emails.', 'wc-gateway-offline' ),
'default' => '',
'desc_tip' => true,
),
) );
}
/**
* Output for the order received page.
*/
public function thankyou_page() {
if ( $this->instructions ) {
echo wpautop( wptexturize( $this->instructions ) );
}
}
/**
* Add content to the WC emails.
*
* @access public
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $this->instructions && ! $sent_to_admin && $this->id === $order->payment_method && $order->has_status( 'on-hold' ) ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
}
/**
* Process the payment and return the result
*
* @param int $order_id
* @return array
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
// Mark as on-hold (we're awaiting the payment)
$order->update_status( 'on-hold', __( 'Awaiting offline payment', 'wc-gateway-offline' ) );
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
WC()->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
} // end \WC_Gateway_Offline class
}


728x90


I am trying to include a php file in a page via

  require_once(http://localhost/web/a.php)

I am getting an error

 Warning: require_once(): http:// wrapper is disabled in the server configuration by   allow_url_include=0

I changed allow_url_include=1 in the php.ini and that worked but I don't think that everybody will let me change their php.ini file.

So, is there any way to accomplish this?

The warning is generated because you are using a full URL for the file that you are including. This is NOT the right way because this way you are going to get some HTML from the webserver. Use:

require_once('../web/a.php');

so that webserver could EXECUTE the script and deliver its output, instead of just serving up the source code (your current case which leads to the warning).

I had this same error while trying to include a PHP file in my Wordpress theme. I was able to get around it by referencing the file name using dirname(__FILE__). I couldn't use relative paths since my file was going to be included in different places throughout the theme, so something like require_once '../path-to/my-file' wouldn't work.

Replacing require_once get_template_directory_uri() . '/path-to/my-file' with require_once dirname( __FILE__ ) . '/path-to/my-file' did the trick.

    try to use

    <?php require_once($_SERVER['DOCUMENT_ROOT'].'/web/a.php'); ?>


    'WEB' 카테고리의 다른 글

    PHP 해당 연,월에 대한 마지막 일자 구하기  (0) 2018.04.27
    [PHP] get_class  (0) 2018.04.23
    php에서 javascript를 호출해보자  (0) 2018.03.19
    Understanding node.js  (0) 2018.02.28
    [PHP] specific float number is removed  (0) 2018.02.21
    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


        + Recent posts