video-downloader/
├── video-downloader.php
├── css/
│ └── style.css
└── js/
└── script.js
<?php
/*
Plugin Name: Video Downloader Tool
Plugin URI: https://yourwebsite.com
Description: A custom WordPress plugin to allow users to download videos via URL.
Version: 1.0
Author: Your Name
License: GPL2
*/
// Enqueue CSS and JS files
function video_downloader_enqueue_scripts() {
wp_enqueue_style('video-downloader-style', plugin_dir_url(__FILE__) . 'css/style.css');
wp_enqueue_script('video-downloader-script', plugin_dir_url(__FILE__) . 'js/script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'video_downloader_enqueue_scripts');
// Add shortcode for the video downloader form
function video_downloader_form() {
ob_start();
?>
<div class="video-downloader-container">
<h2>Download Video</h2>
<form id="video-downloader-form">
<input type="url" id="video-url" name="video-url" placeholder="Enter video URL" required>
<button type="submit">Download</button>
</form>
<div id="video-downloader-result"></div>
</div>
<?php
return ob_get_clean();
}
add_shortcode('video_downloader', 'video_downloader_form');
// Handle the video download via AJAX
function video_downloader_ajax_handler() {
$video_url = esc_url($_POST['video_url']);
// Validate URL format (additional URL restrictions can be added)
if (!filter_var($video_url, FILTER_VALIDATE_URL)) {
wp_send_json_error('Invalid video URL.');
}
// Use a placeholder function to simulate video download.
// Actual implementation requires integrating with a legal video downloading API.
$fake_download_link = 'https://yourwebsite.com/downloads/sample-video.mp4';
wp_send_json_success(['download_url' => $fake_download_link]);
}
add_action('wp_ajax_video_downloader', 'video_downloader_ajax_handler');
add_action('wp_ajax_nopriv_video_downloader', 'video_downloader_ajax_handler');
?>
.video-downloader-container {
max-width: 500px;
margin: 20px auto;
text-align: center;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
#video-downloader-form input {
width: 80%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
#video-downloader-form button {
padding: 10px 20px;
background-color: #0073aa;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
#video-downloader-form button:hover {
background-color: #005f7f;
}
#video-downloader-result {
margin-top: 15px;
font-size: 14px;
color: green;
}
jQuery(document).ready(function ($) {
$('#video-downloader-form').on('submit', function (e) {
e.preventDefault();
const videoUrl = $('#video-url').val();
if (!videoUrl) {
alert('Please enter a valid URL.');
return;
}
$('#video-downloader-result').html('Processing...');
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'video_downloader',
video_url: videoUrl
},
success: function (response) {
if (response.success) {
$('#video-downloader-result').html(
`<a href="${response.data.download_url}" download>Click here to download your video</a>`
);
} else {
$('#video-downloader-result').html(response.data || 'An error occurred.');
}
},
error: function () {
$('#video-downloader-result').html('An error occurred while processing your request.');
}
});
});
});