Force download PDF file from URL in wordpress - wordpress

In WordPress, how do I create a link to a file such as "file.pdf" and force it to download instead of opening the file in the browser?

Just add download attribute ( download) on your a tags and leave it empty as a default file name of a file.
Example:
< a href="http://wordpress.org/download/download.pdf" target="_blank" download>WordPress Download PDF</a>

make download.php in theme folder
<?php
$url = $_REQUEST['file_url'];
$filename = basename($url);
$filetype = filetype($url);
header('Content-Disposition: attachment; filename=' . $filename);
header("Content-type: " . $filetype); // act as image with right MIME type
ob_clean();
flush();
readfile($url);
exit;
then hit anchor link from html page. in this code your file url may be same domain or diffrent domain does not matter
click to download

Related

Wordpress WPAllImport: How to link to a document in the media archive

maybe you can help me,
I tried the code in this Question:
How to download a pdf file and attach it with a custom field in WP All Import Plugin?
function pdf_file_upload($post_id, $att_id, $file) {
$attachment = get_attached_media('application/pdf', $post_id);
foreach($attachment as $attch):
$url = $attch->guid;
update_post_meta($post_id, "__document_file", $url);
endforeach;
}
add_action('pmxi_attachment_uploaded', 'pdf_file_upload', 10, 3);
however, unfortunately this does not work.
When importing from an XML file, the path to the document is transmitted.
The documents are downloaded by specifying the field {wp_u_anlageurl1}
The imported documents are also in the media archive:
How can I link to them in the post when importing?
Greetings Ahmet

Modify uploads directory for gutenberg block

I actually create an extranet space on my website, with pages where you must to be logged-in to see the content.
My problem is about media uploads.
I want to make them private for a specific custom post type, and upload them in a sub directory (uploads/private).
I use the upload_dir filter (add_filter('upload_dir', 'extranet_upload_directory');) with success for thumbnail for exemple, but when I upload an image in the content with a gutenberg block, this one is upload in the initial foldier (uploads), not in uploads/private.
Does anyone have an idea where to look at ?
My code below :
add_filter('upload_dir', 'extranet_upload_directory');
function extranet_upload_directory( $param ){
$id = $_REQUEST['post_id'];
if ( ( get_post_type( $id ) == 'test-extranet' ) ) {
$param['subdir'] = '/private' . $param['subdir'];
$param['path'] = $param['basedir'] . $param['subdir'];
$param['url'] = $param['baseurl'] . $param['subdir'];
}
return $param;
}
This may be a bug... I have a plugin that offers the same functionality as yours and noticed the following:
While creating a post of that new custom post type (e.g. PATHTOsite.fr/wp-admin/post-new.php?post_type=post_type_name):
When I immediately press upload after opening an new image block the uploaded image goes to default directory, uploads.
but, if I first press 'media library' after opening an new image block and then go to the upload button there and upload the image there, the image will be uploaded in the modified uploads directory.

Custom download functionality for wordpress plugin

I am trying to create functionality for a custom made wordpress plugin where when user clicks on the download button appropriate file from the directory should be downloaded related to that post.
I don't want the file to be directly accessible from the URL and want only authorize user to be allowed to download file.
class DownloadM{
function __construct(){
}
function setDownload($file){
//$file = ROOT_DIR_PATH."wp-content/uploads/2016/07/PDF.zip";
echo "<a href='".$file."'>Click here to download</a>";
ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=PDF.zip");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
flush();
ob_clean();
readfile($file);
}
}
The file is properly getting downloaded if I placed the link in the href tag or accessed it from the URL directly.
But when I put the file in the header to auto download it doesn't work
Simple solution for you:
header("Content-Type: application/force-download");

Fetch image from specific directory in drupal

I'm beginner in drupal. I would like to fetch images from specific folder in drupal. Is there any way to fetch drupal images directly from any specific in custom theme.
You should use only images from your theme. And you can get path to your theme like this:
$theme = drupal_get_path('theme',$GLOBALS['theme']);
Put that i.e. at top of template file that uses theme images. And then inside your theme templates you can have something like:
<img src="/<?php echo $theme; ?>/images/my_image.png">
If you stored you theme images inside images dir...
If an array of image paths on your local filesystem is what you want - use this code, it should do what you need. gets all the png jpg gif paths within the sites/default/files/vegas directory.
//We will use the stream wrapper to access your files directory
if ($wrapper = file_stream_wrapper_get_instance_by_uri('public://')) {
//Now we can easily get an actual path to that folder
$directory = $wrapper->realpath();
//Don't forget to append your "vegas" subfolder here
$directory .= DIRECTORY_SEPARATOR . 'vegas' . DIRECTORY_SEPARATOR;
$images = glob($directory . "*.{jpg,png,gif}", GLOB_BRACE);
foreach($images as $imagePath)
{
//Do your thing!
echo $imagePath;
}
}

How to translate a WordPress plugin in any language?

I have completed my plugin, now want to provide a multilingual features for my users. I goggled about it, but it's hard to implement.
I've seen WordPress translation but need basic steps to follow and translate my plugin.
I have done these
downloaded POEdit.
created 'french.po' file in plugin dir
complied 'french.po' -> 'french.mo'
Need to do
How to define msgid & msgstr in po file?
How to load po/mo file in plugin?
How to replace labels/text through po/mo file?
how to use __e() & ___() to replace 'msgstr' in plugin pages?
With a plugin called, Codestyling Localization, you don't need to use POEdit.
I'll show you an example of using 'localizationsample' as the text domain. In this case, the language files are in the /lang/ directory. They don't need to be those names in your actual plugin; they are just examples.
Steps
Add these lines in the plugin comment header to be recognized by Codestyling Localization.
Text Domain: localizationsample
Domain Path: /lang
Create a directory named lang in your plugin directory.
Install and activate the Codestyling Localization plugin.
Go to Tools -> Localization
Find your plugin and click on Add New Language
Select the language (country) to localize in the radio button and press Create po-file At this point make sure a .po file is created in the lang folder.
Press Rescan -> scan now This is recommended since in my system without doing this, the plugin always shows an error saying "not all items are using the same text domain."
Press Edit This will bring you another page listing the messages available to be translated. Those messages are the ones passed to the functions __() and _e() in the plugin code.
Click on Edit in the table next to Copy then you'll get a dialog box to type your translation for each message. Finish translating.
Press generate mo-file At this point, you should see a .mo file being created in the lang folder.
Change your locale specified in wp-config.php to reflect the translation. The default is define('WPLANG', '');
Sample plugin
/*
Plugin Name: Sample Localization
Description: Demonstrates how to localize your plugin.
Text Domain: localizationsample
Domain Path: /lang
*/
// Localization
add_action('init', 'localizationsample_init');
function localizationsample_init() {
$path = dirname(plugin_basename( __FILE__ )) . '/lang/';
$loaded = load_plugin_textdomain( 'localizationsample', false, $path);
if ($_GET['page'] == basename(__FILE__) && !$loaded) {
echo '<div class="error">Sample Localization: ' . __('Could not load the localization file: ' . $path, 'localizationsample') . '</div>';
return;
}
}
// Add Admin Menu
add_action('admin_menu','localizationsample_menu');
function localizationsample_menu() {
add_options_page(
'Localization Demo', // admin page title
'Localization Demo', // menu item name
'manage_options', // access privilege
basename(__FILE__), // page slug for the option page
'localization_demo_adminpanel' // call-back function name
);
}
function localization_demo_adminpanel() {
echo '<div class="wrap"><div id="icon-themes" class="icon32"></div>';
echo '<h2>' . __('Hi there!', 'localizationsample') . '</h2>';
echo '<p>';
_e('Hello world!', 'localizationsample');
echo '</p>';
echo '</div>'; // end of wrap
}
(THIS EXAMPLE IS A TRANSLATION to DEUTCH. YOU CAN CHANGE the customs to YOUR DESIRED names.)
in every plugins head, there is an unique name.
(for example:
/*
Plugin Name: my-pluginname
.......
*/
then, in that plugin's folder, create a folder "languages";
then, into your plugin .php file (somewhere in the top), insert the initialization code:
class load_language
{
public function __construct()
{
add_action('init', array($this, 'load_my_transl'));
}
public function load_my_transl()
{
load_plugin_textdomain('my-pluginname', FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
}
}
$zzzz = new load_language;
then open any text editor, then insert like this code (NOTE, THAT we are only adding two sample messages, "hello" and "bye", so , you can ADD AS MANY messages AS YOU WANT with the similar lines).
# English translations for PACKAGE package.
# Copyright (C) 2012 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# Automatically generated, 2012.
#
msgid ""
msgstr ""
"Project-Id-Version: my-pluginname 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-08-06 13:46-0400\n"
"PO-Revision-Date: 2013-03-21 11:20+0400\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"X-Poedit-SourceCharset: iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 1.5.4\n"
#: mypluginindex.php:87 (it is just a line of a note, to remind where our code appears)
msgid "mymessage1"
msgstr "Hello"
#: mypluginindex.php:88
msgid "mymessage2"
msgstr "Bye"
then save this file as "my-pluginname-en_US.po" (note, that .po is an extension of file, so check that your text editor program has not saved to "my-pluginname-en_US.po.TXT").
then download POEDIT software, and open this file. then edit the "translation" field, and then save as "my-pluginname-de_DE"
there will be generated two files ( If poEdit does not generate the second .mo file automatically, just go to File -> Preferences -> Editor and check the box that says "Automatically compile .mo file on save"),
then put those two file into "languages" folder.
after this, open wp-config.php and find this code:
define ('WPLANG, '');
and change to
define ('WPLANG, 'de_DE');
thats all.
when wordperss is loaded, it will read your plugins language file, with prefix -de_DE.
so, in the plugin's .php file, instead of:
echo "Something string";
you should use:
echo __("mymessage1", 'my-pluginname');
Finished. Now you should test your plugin.
p.s.used links:
https://codex.wordpress.org/I18n_for_WordPress_Developers
http://codex.wordpress.org/Translating_WordPress
https://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Installing_WordPress_in_Your_Language

Resources