0

I'm a WordPress beginner, and I have tried to develop plugins, but I don't know how to call external functions in a WordPress plugin. For example, I have an external PHP script called download.php in which there are several functions. To include this file, I call require_once (download.php);, but then I don't know how to call functions within download.php. For example, I want to call a function download ($filename, $time, $ban_user), where: $filename is a file path, $time is from 1 to 60 minutes, $ban_user is a user id banned. To do that, I coded

add_shortcode ('dloader', 'download_file');
function download_file ($atts){ 
    // I don't know how to code continued here //
}

Please guide me the way of coding, I am not a professional programmer.


add_shortcode( 'dloader', 'download_file' );

// generate shortcode [dloader filename="http://google.com"]
// the result is <a href="http://myweb.com/123456abc/">Download</a>
// where: 123456abc is the crypted parameter for the url http://google.com

function download_file( $atts, $content = null ) {
    require_once ( 'download.php' );
    // the original function: download ( $filename, $time, $ban_user ) { }
    extract( shortcode_atts( array(
        'filename' => '',
        'time'     => '',
        'ban_user' => '',
    ), $atts ) );
    if ( empty( $filename ) ) {
        return 'ERROR';
    }
    if ( empty( $time ) ) {
        $time = 60;
    }
    if ( empty( $ban_user ) ) {
        $ban_user = 0;
    }
    // value 0 is default, not banned
    $default = '<div class="dloader">';
    //execute the function download() in the file download.php
    $default .= '<a href="' .download( $filename, $time, $ban_user ). '">Download</a>';
    return $default;
}
4
  • The question is where do you want to execute function download_file and when do you want to execute it?
    – Omar Tariq
    Dec 18, 2015 at 9:43
  • I mean function download not download_file.
    – Omar Tariq
    Dec 18, 2015 at 10:01
  • I don't know how to code inside the function download_file
    – samdi
    Dec 18, 2015 at 10:18
  • There is no database connection, and the functions work independently, or relate with other parameters inside the file download.php
    – samdi
    Dec 18, 2015 at 10:38

1 Answer 1

1

Generally we use shortcodes when we have to do something in WordPress posts and pages. Though they can also be used elsewhere. Assuming that you know how to use shortcodes here is what you might want to do:-

<?php
/**
 * Plugin Name: Learning To Code WordPress
 */

defined('ABSPATH') or die("No script kiddies please!");

require_once ( dirname(__FILE__) . "/download.php");

add_shortcode ('dloader', 'download_file');
function download_file($atts) { 

    /* Default filename. */
    $default['filename'] = 'defaultFileName.txt';
    /* Default time. */
    $default['time'] = 0;
    /* Default user id. */
    $default['ban_user_id'] = 1;

    $atts = shortcode_atts( $default , $atts, 'dloader' );

    $filename = $atts['filename'];
    $time = $atts['time'];
    $ban_user_id = $atts['ban_user_id'];

    return download($filename, $time, $ban_user_id);

}

Now use the shortcode in a post or a page like this:-

[dloader filename="abc.txt" time=10 ban_user_id=1]
8
  • I want to execute the function download again in the new plugin. The function includes 3 parameters: $URL, $TIME, $IP
    – samdi
    Dec 18, 2015 at 13:25
  • When I execute the function download ('http://goo.gl', 60, 0) this shows http://mypage.com/123abc where 123abc is encrypted string for url http://goo.gl
    – samdi
    Dec 18, 2015 at 13:31
  • @samdi If your download function is not working properly then this issue is outside the scope of this question and this website. You need to put your download function issue on stackoverflow.com.
    – Omar Tariq
    Dec 18, 2015 at 13:34
  • My purpose is to execute any functions in the new plugin, by example, I can imitate the code for other codes
    – samdi
    Dec 18, 2015 at 13:37
  • I think that to better explain you, you need to be more specific what you want to achieve rather than asking how you can execute any function in your plugin. Because if you can understand the plugin I've written above, you can learn to execute any function you want.
    – Omar Tariq
    Dec 18, 2015 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.