1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Upload plugin to wordpress with php and curl

Discussion in 'Other Languages' started by handmadebots, Jul 13, 2014.

  1. handmadebots

    handmadebots Senior Member

    Nov 8, 2012
    Likes Received:
    Home Page:
    I am trying to upload a plugin to my wordpress using php and curl, but it looks like everything I tried is just not working.

    Here is my code:
    function uploadPlugin($filenameWithFullPath) {
            if (!function_exists('curl_init') || !function_exists('curl_exec')) {
                echo 'curl is not available!';
            $visitHost = urlencode($this->wpAdminUrl . '/plugin-install.php?tab=upload');
            $postHost = $this->wpAdminUrl . '/update.php?action=upload-plugin';
            //$postData = "log=$username&pwd=$password&wp-submit=Log%20In&redirect_to=$visitUr";
            $postData = "log=$this->username&pwd=$this->password&wp-submit=Log+In&redirect_to=$visitHost";
            //echo $postData . '<br><br>';
            $ch = curl_init();
            //Url to use for login
            curl_setopt($ch, CURLOPT_URL, $this->loginUrl);
            //Activate cookiesession
            curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
            //Set cookie
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile);
            //No need for SSL
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            //User agent
            curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_USERPWD, 'skywp:119wp');
            //Follow redirection
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            //Return or echo
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_REFERER, $this->loginUrl);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
            curl_setopt($ch, CURLOPT_POST, 1);
            //Save result to content
            $content = curl_exec($ch);
            //echo $content;
            $matches = array();
            $wponce = "";
            if (preg_match('#id="_wpnonce" name="_wpnonce" value="(.*)" /><input type="hidden"#', $content, $matches)) {
                $wponce = $matches[1];
            $matches = array();
            if (preg_match('#<input type="hidden" name="_wp_http_referer" value="(.*)" />#', $content, $matches)) {
                $wpreferer = $matches[1];
            curl_setopt($ch, CURLOPT_URL, $postHost);
            //Interesting thing. The parameter install-plugin-submit shows in firebug but doesn't show up with live http headers
            //Without extra parameter
            //$postData = "_wpnonce=$wponce&_wp_http_referer=$wpreferer&pluginzip=$filenameWithFullPath";
            //With extra parameter
            $cfile = curl_file_create($filenameWithFullPath);
            $postData2 = array(
                //'pluginzip' => '@' . $filenameWithFullPath,
                'pluginzip' => $cfile,
                '_wp_http_referer' => $wpreferer,
                '_wpnonce' => $wponce,
                'install-plugin-submit' => 'Install Now'
            //$postData2 = "_wpnonce=$wponce&_wp_http_referer=$wpreferer&[email protected]$filenameWithFullPath&install-plugin-submit=Install Now";
            //curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData2));
            curl_setopt($ch, CURLOPT_POST, 1);
            $content = curl_exec($ch);
            //var_dump( $content );
            //Close curl
            return $content;
    I also asked the same question on stackoverflow, but no response.
    Here is the link: http://stackoverflow.com/questions/24534103/upload-plugin-to-wordpress-with-php-and-curl

    Any ideas from the php pros ?
  2. MrBlue

    MrBlue Senior Member

    Dec 18, 2009
    Likes Received:
    Web/Bot Developer
    Wow such complications. It would be much simpler to use SSH + SCP to upload and extract the files into your plugin folder.

    # move plugin folder tarball to server
    scp pluginfiles.tar [email protected]:/path/to/your/wp/plugin/folder/
    # untar files into wp plugin folder
    ssh [email protected] tar zxvf /path/to/your/wp/plugin/folder/pluginfiles.tar -C /path/to/your/wp/plugin/folder/
    You can also call these shell commands directly from PHP using shell_exec
    shell_exec('scp pluginfiles.tar [email protected]:/path/to/your/wp/plugin/folder/');
    Last edited: Sep 13, 2014