custom post types - why is my plugin code not saving the meta field to the database

admin2025-01-08  3

I'm trying to write a plugin that will pull pdfs from a set URL, but the meta field isn't saving. Here is my code:

<?php
/*
Plugin Name: Custom PDF Upload
Plugin URI: /
Description: Allows admins to upload PDFs and users to download them with reCAPTCHA verification.
Version: 1.0
Author: Your Name
Author URI: /
License: GPLv2 or later
Text Domain: custom-pdf-upload
*/

// Create a custom post type for PDFs
function create_pdf_post_type() {
    register_post_type( 'pdf', array(
        'labels' => array(
            'name' => 'PDFs',
            'singular_name' => 'PDF',
        ),
        'public' => false,
        'show_ui' => true,
        'has_archive' => false,
        'supports' => array( 'title', 'editor', 'thumbnail' ),
    ) );
}
add_action( 'init', 'create_pdf_post_type' );

// Add a custom field for PDF file URL
function add_pdf_url_field( $post_id ) {
    if ( get_post_type( $post_id ) == 'pdf' ) {
        echo '<label for="pdf_url">PDF URL:</label>';
        echo '<input type="text" id="pdf_url" name="pdf_url">';
    }
}
add_action( 'edit_form_after_editor', 'add_pdf_url_field' );

function save_pdf_url( $post_id ) {
    if ( isset( $_POST['pdf_url'] ) && !empty( $_POST['pdf_url'] ) ) {
        update_post_meta( $post_id, 'pdf_file_url', $_POST['pdf_url'] );
    }
}
add_action( 'save_post', 'save_pdf_url' );

My custom post type is created and there is a meta field under my content section but when i enter something into the field and hit save the edit page refreshes and the url is not saved. Content does get saved though.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268051a1243.html

最新回复(0)