How backup automated process to my WordPress Sites using code? - wordpress

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?

Related

Save array of images to user in Wordpress?

Is it possible to save array of images to the user?
I already created a form and I'm at the saving part.
I already have this code:
update_user_meta($user->ID, 'gallery', $_POST['gallery_images']);
gallery_images contains the array of input images in the form.
And I know this is not working. Is it possible to save an array of images in user? If possible, how?
PS.
I'm using the latest version of wordpress.
(Revised answer)
As pointed in the comment to the other answer, you can use media_handle_upload() to upload the images, but since the function only supports single upload, then for multiple uploads, you can set a temporary $_FILES item like so:
// Load upload-related and other required functions.
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$post_id = 0; // set to the proper post ID, if attaching to a post
$uploaded = []; // attachment IDs
foreach ( $_FILES['gallery_images']['tmp_name'] as $key => $file ) {
// Set a temporary $_FILES item.
$_FILES['_tmp_gallery_image'] = [
'name' => $_FILES['gallery_images']['name'][ $key ],
'type' => $_FILES['gallery_images']['type'][ $key ],
'size' => $_FILES['gallery_images']['size'][ $key ],
'tmp_name' => $file,
'error' => $_FILES['gallery_images']['error'][ $key ],
];
// Upload the file/image.
$att_id = media_handle_upload( '_tmp_gallery_image', $post_id );
if ( ! is_wp_error( $att_id ) ) {
$uploaded[] = $att_id;
}
}
unset( $_FILES['_tmp_gallery_image'] );
// Save the attachment IDs.
$user = wp_get_current_user();
update_user_meta( $user->ID, 'gallery', $uploaded );
And I'm saving the attachment IDs, but of course, it's up to you if you'd rather save the image URLs, etc.
PS: You can check the original answer here to see how you can also upload the images using media_handle_sideload(). (It works well, but instead of going through a wrapper (function), we should just call media_handle_upload() unless if you're "uploading" external/remote image/file.) Sorry about that answer.. :)
You can serialize the data, and when you need it unserialize it.
update_user_meta($user->ID, 'gallery', serialize($_POST['gallery_images']));
serialize:
https://www.php.net/manual/fr/function.serialize.php

How to show the title of the embedded audio file in WordPress

audio_list(get_the_content())
function audio_list($data){
$content = strip_tags($data);
$audiolist = explode("[/audio]", $content);
$audiolist = array_filter($audiolist);
return $audiolist;
}
Right now am using a custom function that strip the audio url and split the file name from the url and showing the real file name as the name of the audio in my site. I need to change the real name as media title. How can i do this ?? In word press embedded player media id is not available. In bulk upload case media is not associated with any posts so parent post attachment concept also not working. Do have any other method to get the media title ??
Get the uploaded audio files as an array first
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'audio',
'numberposts' => -1
);
$audiofiles = get_posts($args);
create a if condition that matching the file url's
foreach ($audiofiles as $file)
{
$urls = wp_get_attachment_url($file->ID);
if($urls == 'url from the content'){
$title = $file->post_title;
}else {
$title = 'file real name';
}
}

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 can I put these codes into function.php in WordPress?

I want to upload file with Chinese character to a WordPress website. However, the name of the file is gibberish. I have searched the question and found some solutions. One is to modify the //wp-admin/includes/file.php
function wp_handle_upload( &$file, $overrides = false, $time = null ) {
//….
// Move the file to the uploads dir
//$new_file = $uploads['path'] . “/$filename”;
$new_file = $uploads['path'] . “/” . iconv(“UTF-8″,”GB2312″,$filename);
//…
//return apply_filters( ‘wp_handle_upload’, array( ‘file’ => $new_file, ‘url’ => $url, ‘type’ => $type ), ‘upload’ );
return apply_filters( ‘wp_handle_upload’, array( ‘file’ => $uploads['path'] . “/$filename”, ‘url’ => $url, ‘type’ => $type ) , ‘upload’);
}
As I know, it changes the encoding of the filename. But I want to put it in the function.php in the theme folder. What can I do ?

Buddypress bp_activity_add(activity_action) hides the link "target"

I'm developing a social network with Buddypress, I created a RSS plugin to pull the RSS feed from the specified websites.
Everything is working, except when the RSS is posted to the activity stream. When I create the activity content to print a link, I set the link target to "_new" to open it in a new page.
Here's the code:
function wprss_add_to_activity_feed($item, $inserted_ID) {
$permalink = $item->get_permalink();
$title = $item->get_title();
$admin = get_user_by('login', 'admin');
# Generates the link
$activity_action = sprintf( __( '%s published a new RSS link: %s - ', 'buddypress'), bp_core_get_userlink( $admin->ID ), '' . attribute_escape( wprss_limit_rss_title_chars($title) ) . '');
/* Record this in activity streams */
bp_activity_add( array(
'user_id' => $admin->ID,
'item_id' => $inserted_ID,
'action' => $activity_action,
'component' => 'rss',
'primary_link' => $permalink,
'type' => 'activity_update',
'hide_sitewide' => false
));
}
It should come up with something like that:
Test
But it prints like that:
Test
Why is this happening?
The 'target' attribute is probably getting stripped by BuddyPress's implementation of the kses filters. You can whitelist the attribute as follows:
function se16329156_whitelist_target_in_activity_action( $allowedtags ) {
$allowedtags['a']['target'] = array();
return $allowedtags;
}
add_filter( 'bp_activity_allowed_tags', 'se16329156_whitelist_target_in_activity_action' );
This probably won't retroactively fix the issue for existing activity items - it's likely that they had the offending attribute stripped before being stored in the database. But it should help for future items.

Resources