Is file_get_contents() the only way for plugins reading local files OR does WP_Filesystem_Direct::get_contents() even work?

Background I'm developing a plugin for WordPress. I initially used file_get_contents() for two purposes: A remote file (which I changed to wp_remote_post()) A local file in the plugin dir (what this question is about) WordPress staff told me to NOT use file_get_contents() for remote _POST requests (usage 1. above), but to use wp_remost_post() instead. From their email concerning usage 1. (above): Using file_get_contents on remote files Many hosts block the use of file_get_contents on remote content. This is a security …
Category: Web

unzip_file not working with the remote file

I'm trying to use unzip_file to extract the remote archive with no success. It works only with the local path using get_template_directory() but with a remote zip archive URL I'm getting Incompatible Archive. message. Here is the stripped version of my code: WP_Filesystem(); global $wp_filesystem; $source = 'http://downloads.wordpress.org/theme/ona-creative.1.0.0.zip'; $unzipfile = unzip_file( $source, get_theme_root() ); if ( is_wp_error( $unzipfile ) ) { wp_send_json( array( 'done' => 1, 'message' => esc_html__( $unzipfile->get_error_message() . ' There was an error unzipping the file.', 'ona' …
Category: Web

PHP File_exist() not working - Checking if File Exist in WordPress Theme Directory

I am trying to check if a certain archive file template exists in the WP theme directory. This is my code, I am not sure what I am missing here. <?php $file_name = '/archive.php'; $base_template_dir = get_template_directory_uri(); $file_uri = $base_template_dir.$file_name; $file_uri = str_replace('http://', '', $file_uri); $file_uri = str_replace('https://', '', $file_uri); $file_uri = str_replace($_SERVER['HTTP_HOST'], '', $file_uri); if (file_exists($file_uri)) { echo 'File Found: '.$file_uri; } else { echo 'File Not Found: '.$file_uri; } ?>
Category: Web

how to get json file in wordpress template

I am trying to get data from JSON file in Wordpress template using file_get_contents But can not get the file $data = file_get_contents("../assets/data2.json"); $arr = json_decode($data, true); Facing this error while var_dump $data object(WP_Error)#825 (2) { ["errors"]=> array(1) { ["http_request_failed"]=> array(1) { [0]=> string(29) "A valid URL was not provided." } } ["error_data"]=> array(0) { } } My data2.json file is in the assets folder and trying from templates folder in theme folder in wordpress.
Category: Web

wp_filesystem put_contents issue with owner/group

I am using put_contents() function to put images on the server, works fine on localhost, though on server, the user group when the file are downloaded to server is "nginx nobody" and that is causing the images to return 404 error and not to be viewed, how can I make the file be downloaded with the default WordPress owner/group global $wp_filesystem; // Initialize the WP filesystem. if (empty($wp_filesystem)) { require_once (ABSPATH . '/wp-admin/includes/file.php'); WP_Filesystem(); } $wp_filesystem->put_contents($save_file_to, $file_to_save, 0644)
Category: Web

Setting wp_temp_dir and permissions not working for "Missing A Temporary Folder" error

I have a problem uploading files to my main webpress site (e.g. www.main.com) and all the other pages that live in it (e.g. www.main.com/anotherpage). I know there is a method to set a temp folder like define( 'WP_TEMP_DIR', dirname(__FILE__) . '/wp-content/temp/' ); but I have already put this line in the wp-config.php file but I did have any success. Also I have already changed all permissions to the folders using (maybe is better to use 775, but I wanted to …
Category: Web

How to prevent users to view server files using WP File Manager plugin?

I'm developing some plugins to an website, but I don't want the users to see the code (or download it). If the user install the plugin WP File Manager, all files in the host will be revealed to him. Plugins are an essencial part in the WP ecossystem and I don't want to remove the ability of the users to install. I just don't want the users to access the core files. I tried this approach: Change the file owner, …
Category: Web

Submitting form from input[type=file] dialog box

I am attempting to trigger a form submit from an input [type=file] dialog box window. I realize this might be a bit substandard, but here's what I'm trying to do. User clicks the browse button, and input[type=file] dialog window is invoked letting the user select a file. After the user selects file and when the open button is pressed in the same window, the file dialog box (in addition to whatever else it does), submits the specified form (jQuery perhaps?) …
Category: Web

How do you use unzip_file()?

I have been trying to use the unzip_file() function. It says undefined so I looked into it and the WP_Filesystem() must be called and set up. So easy, right? require_once(ABSPATH .'/wp-admin/includes/file.php'); WP_Filesystem(); unzip_file( $zip, $dest ); Even this shows as undefined, and I don't see any documentation on it. I am trying to use the unzip feature to unpack a separate plugin during theme install.
Category: Web

How to saving a file to Media Libary by code

I'm building a plugin that works only in admin. I've a page where the user choose some parameters and the script get data from posts and posts meta then it generate a pdf file and then I want to save it or in a specif folder or in the media library adding it to a specific Media Category. My questions is: How can I save by code the generated pdf file to Media Library with a specific Media Category? Regards. …
Category: Web

Admin Custom Plugin List Files in admin Table

I'm building a plugin that works only in admin. I've a page where I need a list of files, users can only download or delete the file. I've this questions: I don't know what is better to list the files in a specific folder or files saved in media library associated a specific category? How can I get files list from a specific folder? How can I get files list form a specific media library category? How can I put …
Category: Web

How to include form for WP_Filesystem()?

I've used a bit of code to try and write a dynamic.css file to a theme folder and it works fine. BUT, it doesn't work on certain shared hosting situations so the request_filesystem_credentials() function is invoked. This produces a really ugly ftp form with no connect/submit button. I then followed the great Otto's tutorial on this to try and use a decent form instead but the end result is a nice looking form that gives the error "Error saving file!" …
Category: Web

How can I add a method to create files when in theme-editor.php

This is a concept of my goal I want to be able to create files in the current opened directory by entering the complete file name and and extension in a text field, click create and use the AJAX process to run the file_put_contents() method. I haven't tried any coding methods as I do not even have a starting point. After 18 hours of research, I have only found methods using add_theme_page() to add a page where all the custom …
Category: Web

Register visits of my pages in wordpresss

I have a shortcode that obtains the name of the person logged into wordpress, I would like that shortcode when the person enters a page where I have my shortcode create a .text file and save their name inside it this is my wordpress shortcode function alertaLogin( $atts ) { global $current_user, $user_login; wp_get_current_user(); add_filter('widget_text', 'apply_shortcodes'); if ($user_login) return 'hola ' . $current_user->display_name; else return 'no ha iniciado session'; } add_shortcode( 'shortcode_login', 'alertaLogin' ); $contenido = 'saludo'; $archivo = fopen('archivo.txt','a+'); …
Category: Web

Unzip_file causing Media file upload error

When I attempt to use the unzip_file function below in my functions.php: require_once(ABSPATH . 'wp-admin/includes/file.php'); WP_Filesystem(); $destination = wp_upload_dir(); $destination_path = $destination['path']; $unzipfile = unzip_file( $destination_path.'/filename.zip', $destination_path); if ( is_wp_error( $unzipfile ) ) { echo 'There was an error unzipping the file.'; } else { echo 'Successfully unzipped the file!'; } I get the following error when trying to upload any files into the Media library in Media > Add New: An error occurred in the upload. Please try again …
Category: Web

How do you find a file in the media library using the file URL?

We have about 2000 files in our media library and we are working on an audit to organize the files and create a name hierarchy. But until that is done, I am trying to locate a handful of specific files to replace. I don't know what the files are named in Wordpress, I only have the URL to download the file. Is there a way to search for the file in the dashboard using the URL?
Category: Web

Unable to check if image uploaded by wp_image_editor exists using file_exists function

I am developing a podcast theme where i have a form that allows the user to upload their podcast cover arts. The Podcast cover art’s dimension should fall under 1400×1400 and 3000×3000 since we are following iTune’s regulations. Since such big files will impact the page load speed of the site i wanna resize the uploaded image to a smaller size, say 250×250 so that we can use that to display on our web pages. I have used WP_Image_Editor::resize function …
Category: Web

What dependencies should I load and to use the WP_Filesystem?

I'm building a Wordpress theme which has a WP_Filesystem call in it: <?php $wp_filesystem = new WP_Filesystem_Direct(null); echo $wp_filesystem->get_contents(get_stylesheet_directory() . '/assets/images/Search_Glyph.svg'); ?> I know that the WP_Filesystem needs some dependencies. I load them now like this in functions.php: require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; I don't know if this is the correct way to load the dependencies for the filesystem. I found an other question for plugin development and after some research I made the following code, which …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.