make user upload image to in theme folder - wordpress

i am developing a wordpress theme. i want to make my users' images saved in NOT https://example.com/wp-content/uploads folder, but in https://example.com/wp-content/themes/mytheme/images folder.
i added this code to my themes' functions.php file :
function my_custom_upload_dir( $dir ) {
return array(
'path' => WP_CONTENT_DIR . '/portfolio/images',
'url' => WP_CONTENT_URL . '/portfolio/images',
'subdir' => '/portfolio/images',
) + $dir;
}
add_filter( 'upload_dir', 'my_custom_upload_dir' );
and i got this error :
Unable to create directory wp-content/uploads/portfolio/images. Is its parent directory writable by the server?
i dont want to save in wp-content/uploads/portfolio/images file, i want to save in wp-contect/themes/mytheme/images file. how can i solve this problem?

Related

How backup automated process to my WordPress Sites using code?

I have used the code below on the c-panel WordPress repository and created a backup folder in the public HTML folder. But not working on I need to know code issue and where I should customize to get my site automatedly by using code.
`function create_backup() {
// Get the site's URL and name
$site_url = get_site_url();
$site_name = get_bloginfo('name');
// Create a filename for the backup using the site's URL and current date
$filename = $site_name . '-' . $site_url . '-' . date('Y-m-d') . '.zip';
// Create a ZIP archive of the site's files
$zip = new ZipArchive;
$zip->open($filename, ZipArchive::CREATE);
$zip->addGlob(ABSPATH . '/*');
$zip->close();
// Send the ZIP archive to a remote location
// Replace "example.com" with the URL of your remote location
$response = wp_remote_post('http://example.com/backups/', array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/zip'
),
'body' => file_get_contents($filename),
));
}`
Use the wp_schedule_event() or wp_schedule_single_event() function to schedule the custom function to run at a specific time or interval.
And use this Bloew of this code in functions.php
`if ( ! wp_next_scheduled( 'create_backup_event' ) ) {
wp_schedule_event( time(), 'daily', 'create_backup_event' );
}
add_action( 'create_backup_event', 'create_backup' );`
Please notify me where I need to add my domain name and customization needs!
I have tried this on my Name Cheap Hosting. But My inquiry is where should I need to create the Back Folder in the Public Html folder or anywhere else?

Placing Wordpress plugin name at the top of the amdin header

I have a Wordpress plugin which I've named 'Solve Maths'. I want this plugin name to be displayed only at the Admin header of the Arthur alone for easy access.
I used the admin_head action hook but once I installed the plugin, the system tells me my plugin has generated these number of characters at the header.
<?php function Solve_Maths{ echo "<a href='Solve_Maths.php'>Solve Maths</a>";} add_action('admin_head','Solve_Maths');?>
The Solve_Maths.php is the name of the main plugin file with the header information. I want this file name in the tag to be shown at the admin header of the user and should execute the file when the link is linked. Thank you all for your help.
This is not the correct way to do it. Please check the WordPress Codex and refer to the function add_node: https://codex.wordpress.org/Function_Reference/add_node
add_action( 'admin_bar_menu', 'toolbar_link_to_mypage', 999 );
function toolbar_link_to_mypage( $wp_admin_bar ) {
$args = array(
'id' => 'my_page',
'title' => 'My Page',
'href' => 'http://example.com/my-page/',
'meta' => array( 'class' => 'my-toolbar-page' )
);
$wp_admin_bar->add_node( $args );
}

Drupal 7: drupal_add_js() is not adding .js file

I want to add a .js file to my site and have it appear in the <head>
To do this I added the following to the theme's template.php file:
function mytheme_preprocess_page(&$variables) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/myfile.js', array(
'type' => 'file',
'group' => JS_THEME,
'scope' => 'header',
));
}
But nothing appears when I check the source code (note the .js file is in the theme root directory).
I also tried
drupal_add_js('jQuery(document).ready(function () { console.log("Hello!"); });', 'inline');
But got nothing.
Would anyone know why this is occuring and what I could do?

Tinymce 4 Elfinder absolute path to image

I've integrated elfinder v2.1 with TinyMce4.
It works great except that the path to the image elfinder give to tinymce is not correct.
The simpliest solution would be to use absolute path. I found some resources to do that with previous version of tinymce (elfinder + tinymce3) but not with version 4.
I try to change some variables in connector.php but without any success.
Here is the actual code:
<?php
error_reporting(0); // Set E_ALL for debuging
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderConnector.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinder.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeDriver.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeLocalFileSystem.class.php';
// Required for MySQL storage connector
// include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeMySQL.class.php';
// Required for FTP connector support
// include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeFTP.class.php';
/**
* Simple function to demonstrate how to control file access using "accessControl" callback.
* This method will disable accessing files/folders starting from '.' (dot)
*
* #param string $attr attribute name (read|write|locked|hidden)
* #param string $path file path relative to volume root directory started with directory separator
* #return bool|null
**/
function access($attr, $path, $data, $volume) {
return strpos(basename($path), '.') === 0 // if file/folder begins with '.' (dot)
? !($attr == 'read' || $attr == 'write') // set read+write to false, other (locked+hidden) set to true
: null; // else elFinder decide it itself
}
$opts = array(
// 'debug' => true,
'roots' => array(
array(
'driver' => 'LocalFileSystem', // driver for accessing file system (REQUIRED)
'path' => '../files/', // path to files (REQUIRED)
'URL' => dirname($_SERVER['PHP_SELF']) . '/../files/', // URL to files (REQUIRED)
'accessControl' => 'access' // disable and hide dot starting files (OPTIONAL)
)
)
);
// run elFinder
$connector = new elFinderConnector(new elFinder($opts));
$connector->run();
?>
Anyone knows how to retrieve an absolute url from elfinder?
Thanks
I found a solution by modifying connector.php file and adding the alias option
see: https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options
So, in function access, go to $opt
and modify URL and alias like this:
$opts = array(
// 'debug' => true,
'roots' => array(
array(
'driver' => 'LocalFileSystem', // driver for accessing file system (REQUIRED)
'path' => '../files/', // path to files (REQUIRED)
'URL' => 'absolutepath/to/elfinder/files/', // URL to files (REQUIRED)
'alias' => 'absolutepath/to/elfinder/files/',
'accessControl' => 'access' // disable and hide dot starting files (OPTIONAL)
)
)
);
Hope it can help.

How to create resized versions of images programatically uploaded with wp_insert_attachment

I'm programmatically uploading images to my Wordpress media gallery and setting them as my post's featured image, which is working quite well.
I'd like to generate thumbnails of these featured images, in the same way that Wordpress handles creating resized thumbnails when an image is added through the Insert Media pane, where it creates multiple files, each with the new dimensions specified in the file name.
Unfortunately, I'm having a lot of trouble locating anything that explains how this would be done properly.
Below is the relevant code. It successfully creates the post and sets the featured image to the media file, but doesn't generate thumbnails to go with it.
Any help would be greatly appreciated!
// If the post was created
if($post_id) {
// Add meta/custom field data
add_post_meta($post_id, 'gif_random_id', $randomId);
// Add uploaded file to the actual Wordpress image library
global $uploadURL;
$filename = $uploadURL . $randomId . '.' . $fileExtension;
$wp_filetype = wp_check_filetype(basename($filename), null);
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'publish'
);
$attach_id = wp_insert_attachment($attachment, $filename, $post_id);
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
// Now set the attachement as the featured image
update_post_meta($post_id, '_thumbnail_id', $attach_id);
}
You should use another - rather unknown and misunderstood function- media_sideload_image()
$attach_id = media_sideload_image($filename, $post_id);
This function will handle the "upload" process and will follow the normal workflow of uploading images to wordpress which includes thumbs generation as defined in your custom image sizes
Kaput, your code is perfectly fine, but seems your initial image is not in the uploads folder. See what the documentation says:
Location of the file on the server. Use absolute path and not the URI
of the file. The file MUST be on the uploads directory. See wp_upload_dir().
Just make sure you upload the image in the uploads directory first.

Resources