简体   繁体   English

当我将文件上传到媒体库时,我的 Wordpress 插件没有创建页面

[英]My Wordpress plugin does not create page when I upload a file to media library

Hey I wrote a custom plugin for my wordpress site which should create a new page with a download link when I upload a file to media library, but it does not seem to work and I am fairly new to php and wordpress ecosystem so I do not know where the problem lies.嘿,我为我的 wordpress 网站写了一个自定义插件,当我将文件上传到媒体库时,它应该创建一个带有下载链接的新页面,但它似乎不起作用,而且我对 php 和 wordpress 生态系统还很陌生,所以我不知道知道问题出在哪里。

Here is the code that I wrote这是我写的代码

<?php
    /**
     * Plugin Name: Automatic Download Page Creator
     * Plugin URI: https://example.com/automatic-download-page-creator
     * Description: Automatically creates a page or post with a download link for a file when it is uploaded to the media library.
     * Version: 1.0
     * Author: Wisecraft
     * Author URI: https://example.com
     * License: GPL2
     */

    // Hook into the 'wp_insert_attachment' action
    add_action( 'wp_insert_attachment', 'create_download_page', 10, 2 );

    // Custom function to create the download page or post
    function create_download_page( $attachment_id, $attachment ) {
        // Get the file URL and post title
        $file_url = wp_get_attachment_url( $attachment_id );
        $post_title = get_the_title( $attachment_id );

        // Create the post content with a download link
        $post_content = '<p>Click the link below to download the file:</p>';
        $post_content = '<p><a href="' . $file_url . '">Download ' . $post_title . '</a></p>';

        // Insert the page or post
        $new_post = array(
          'post_type' => 'post', // or 'post'
          'post_title' => $post_title,
          'post_content' => $post_content,
          'post_status' => 'publish',
        );
        $post_id = wp_insert_post( $new_post );
    }
?>

I expected my plugin to create a new page with a custom content when I upload a file to Media Library in WordPress, but it does not do anything and I cannot find a problem with the code as well.当我将文件上传到 WordPress 中的媒体库时,我希望我的插件创建一个包含自定义内容的新页面,但它什么也没做,我也找不到代码问题。

This code should work.这段代码应该可以工作。 You used wrong hook.你用错了钩子。 And if you want add string to existing you need to use.= operator for $post_content.如果您想将字符串添加到现有内容中,则需要对 $post_content 使用 .= 运算符。 And if you want add Page not Post you need to change post_type in $args array to "page"如果你想添加页面而不是帖子,你需要将 $args 数组中的 post_type 更改为“page”

add_action( 'add_attachment', 'create_download_page', 10, 1 );
function create_download_page( $attachment_id ) {
   // Get the file URL and post title
   $file_url = wp_get_attachment_url( $attachment_id );
   $post_title = get_the_title( $attachment_id );

   // Create the post content with a download link
   $post_content = '<p>Click the link below to download the file:</p>';
   $post_content .= '<p><a href="' . $file_url . '">Download ' . $post_title . '</a></p>';

   // Insert the page or post
   $new_post = array(
     'post_type' => 'page', // or 'post'
     'post_title' => $post_title,
     'post_content' => $post_content,
     'post_status' => 'publish',
   );
$post_id = wp_insert_post( $new_post );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何上传到wordpress媒体库 - How to I upload to wordpress media library 将多部分表单(输入类型=文件)转换为WordPress媒体库上传 - convert multipart form (input type = file) into WordPress media library upload 在上传之前更改$ _FILES文件名(到wordpress媒体库) - Change $_FILES file name before upload (to wordpress media library) 是否可以不上传就将项目添加到wordpress的媒体库中? - Can I add items to the media library of wordpress without upload? wordpress:使用polylang插件的媒体库问题 - wordpress: media library issue with polylang plugin 在Wordpress插件中创建上传字段 - Create upload field in Wordpress plugin 当我单击“媒体”&gt;“ WordPress中的库”时,管理图像未显示为什么? - When i click on Media > library in WordPress admin images are not showing why is that? 当我从媒体库加载图片时,wordpress 5 空站点 - wordpress 5 empty site when i load pictures from media library 为什么在添加functions.php时我的媒体库消失了? - Why does my media library disappear when I add functions.php? 单击任何Wordpress页面上的按钮,如何打开Wordpress Media Library文件夹中的pdf文件 - How to open pdf file which is in Wordpress Media Library folder on click of button on any wordpress page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM