Das Plugin speichert keine Werte, wenn es in einen Ordner innerhalb von / wp-content / plugins gestellt wird

Ich habe den folgenden Code für ein WordPRess-Plugin, das im Wesentlichen zwei benutzerdefinierte Eingaben auf jedem Seiten- / Post-Bearbeitungsbildschirm hinzufügt. Die Werte werden dann gespeichert und in der Kopfzeile der Frontend-Seite ausgegeben.

Der Code funktioniert einwandfrei, wenn er sich in einer .php-Datei befindet und direkt in ‘wp-content / plugins’ eingefügt wird. Wenn ich es jedoch innerhalb von Plugins in einen eigenen Ordner lege, zB “wp-content / plugins / myplugin”, werden die Eingabefelder beim Speichern des Posts / der Seite über den Bearbeitungsbildschirm nicht gespeichert. Zusätzlich gibt es nichts, was auf den HTML-Kopfbereich der Frontend-Seiten ausgegeben wird.

Es scheint, dass dies ein verlassenes Projekt ist, so dass ich nicht in der Lage war, mit dem ursprünglichen Entwickler zu arbeiten, um eine Lösung zu erarbeiten. Es scheint jedoch, dass durch etwas im Code aktualisiert werden muss, um zu reflektieren, dass der Code insicde ein Plugin-Ordner ist.

Heres der Code …..

<?php $clinical_post_types = array(); //add all post types $post_types = get_post_types(); foreach ( $post_types as $post_type ) { $clinical_post_types[] = $post_type; //echo 'POST TYPES: ' .$post_type; } // Meta box // ----------------------------------------------------------------- // Add the language metabox on every registered custom post type function clinical_add_language_metaboxe() { global $clinical_post_types; foreach ($clinical_post_types as $post_type) { add_meta_box('clinical_meta_box', __('Search Engine Optimization', 'clinical'), 'clinical_meta_custom_box', $post_type, 'normal', 'default'); } } // The Post's meta fields Metabox function clinical_meta_custom_box() { global $post; echo ''; // Get the meta fields data if its already been entered $meta_title = get_post_meta($post->ID, '_clinical_meta_title', true); $meta_description = get_post_meta($post->ID, '_clinical_meta_description', true); // Echo out the field $html = '

'; $html .= '

'; $html .= '

'; $html .= '

'; echo $html; } // Save the metabox data function clinical_save_post_meta($post_id, $post) { global $clinical_post_types; $key_title = '_clinical_meta_title'; $key_description = '_clinical_meta_description'; // if we're not in a clinical-enabled post type, skip. if (in_array($post->post_type, $clinical_post_types)) return $post; // verify this came from our screen and with proper authorization, // because save_post can be triggered at other times if ((empty($_POST[$key_title]) && empty($_POST[$key_description])) || empty($_POST['clinicalmeta_noncename']) || !wp_verify_nonce($_POST['clinicalmeta_noncename'], plugin_basename(__FILE__).'/seo_fields.php') || !current_user_can('edit_post', $post->ID) ) { return $post->ID; } // OK, we're authenticated: we need to find and save the data $title = $_POST[$key_title]; $description = $_POST[$key_description]; // set the post's meta title: $updated_title = update_post_meta($post->ID, $key_title, $title); $updated_description = update_post_meta($post->ID, $key_description, $description); // Delete if blank: if (!$title) delete_post_meta($post->ID, $key_title); if (!$description) delete_post_meta($post->ID, $key_description); } // Filters function clinical_wp_title_filter($title) { global $post; $seo_title = get_post_meta($post->ID, '_clinical_meta_title', true); if (!empty($seo_title)) return $seo_title; return $title; } function clinical_wp_head_action() { global $post; $seo_description = get_post_meta($post->ID, '_clinical_meta_description', true); if (!empty($seo_description)) { echo ' '; } } // Helpers // Filters and Hooks add_action('admin_init', 'clinical_add_language_metaboxe'); add_action('save_post', 'clinical_save_post_meta', 1, 2); add_filter('wp_title', 'clinical_wp_title_filter'); add_action('wp_head', 'clinical_wp_head_action');

Solutions Collecting From Web of "Das Plugin speichert keine Werte, wenn es in einen Ordner innerhalb von / wp-content / plugins gestellt wird"

Hier ist der fixe und modifizierte Code, der das Nonce-Aktionsproblem hatte.

 < ?php /* Plugin Name: Test Plugin Plugin URI: Test Description: Test Author: Test Version: 007 Author URI: */ // add all post types $clinical_post_types = array(); $post_types = get_post_types(); foreach ( $post_types as $post_type ) { $clinical_post_types[] = $post_type; } // Meta box // Add the language metabox on every registered custom post type function clinical_add_language_metaboxe() { global $clinical_post_types; foreach ( $clinical_post_types as $post_type ) { add_meta_box( 'clinical_meta_box', __( 'Search Engine Optimization', 'clinical' ), 'clinical_meta_custom_box', $post_type, 'normal', 'default' ); } } // The Post's meta fields Metabox function clinical_meta_custom_box() { global $post; // Get the meta fields data if its already been entered $meta_title = get_post_meta( $post->ID, '_clinical_meta_title', true ); $meta_description = get_post_meta( $post->ID, '_clinical_meta_description', true ); echo ''; // Echo out the field $html = '

'; $html .= '

'; $html .= '

'; $html .= '

'; echo $html; } // Save the metabox data function clinical_save_post_meta( $post_id, $post ) { global $clinical_post_types; $key_title = '_clinical_meta_title'; $key_description = '_clinical_meta_description'; // if we're not in a clinical-enabled post type, skip. if ( in_array( $post->post_type, $clinical_post_types ) ) { // return $post; } // verify this came from our screen and with proper authorization, // because save_post can be triggered at other times if ( ( empty( $_POST[ $key_title ] ) && empty( $_POST[ $key_description ] ) ) || empty( $_POST['clinicalmeta_noncename'] ) || ( ! wp_verify_nonce( $_POST['clinicalmeta_noncename'], "clinicalmeta_nonce_action" ) ) || ( ! current_user_can( 'edit_post', $post->ID ) ) ) { return $post->ID; } // OK, we're authenticated: we need to find and save the data $title = $_POST[ $key_title ]; $description = $_POST[ $key_description ]; // set the post's meta title: $updated_title = update_post_meta( $post->ID, $key_title, $title ); $updated_description = update_post_meta( $post->ID, $key_description, $description ); // Delete if blank: if ( empty( $title ) ) { delete_post_meta( $post->ID, $key_title ); } if ( empty( $description ) ) { delete_post_meta( $post->ID, $key_description ); } } // Filters function clinical_wp_title_filter( $title ) { global $post; $seo_title = get_post_meta( $post->ID, '_clinical_meta_title', true ); if ( ! empty( $seo_title ) ) { return $seo_title; } return $title; } function clinical_wp_head_action() { global $post; $seo_description = get_post_meta( $post->ID, '_clinical_meta_description', true ); if ( ! empty( $seo_description ) ) { echo ' '; } } // Helpers // Filters and Hooks add_action( 'admin_init', 'clinical_add_language_metaboxe' ); add_action( 'save_post', 'clinical_save_post_meta', 1, 2 ); add_filter( 'wp_title', 'clinical_wp_title_filter' ); add_action( 'wp_head', 'clinical_wp_head_action' );

Dies ist ein Problem bei der Erstellung der Nonce.

 wp_create_nonce( plugin_basename(__FILE__) ) . '/seo_fields.php" /> 

sollte wahrscheinlich sein

 wp_create_nonce( plugin_basename(__FILE__) . '/seo_fields.php' ) . '" /> 

weil sonst

 !wp_verify_nonce($_POST['clinicalmeta_noncename'], plugin_basename(__FILE__).'/seo_fields.php') 

würde nicht nach der gleichen Aktion suchen.

Siehe: https://codex.wordpress.org/Function_Reference/wp_verify_nonce#Parameters

Tropfen mic