|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Plugin Name: Quick Weblog |
| 5 | +Plugin URI: https://fexd.com/wordpress/plugins/quick-weblog |
| 6 | +Description: Create new weblog posts quickly and easily. |
| 7 | +Version: 0.0.1 |
| 8 | +Author: Arlin Schaffel |
| 9 | +Author URI: https://fexd.com |
| 10 | +License: GPL2 |
| 11 | +License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 12 | +Text Domain: quick-weblog |
| 13 | +Domain Path: /languages/ |
| 14 | +*/ |
| 15 | + |
| 16 | +function quick_weblog_form() { |
| 17 | + ?> |
| 18 | + <form method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>"> |
| 19 | + <label for="title"><?php _e( 'Title', 'quick-weblog' ); ?></label> |
| 20 | + <input type="text" name="title" id="title" required> |
| 21 | + |
| 22 | + <label for="image_url"><?php _e( 'Image URL', 'quick-weblog' ); ?></label> |
| 23 | + <input type="text" name="image_url" id="image_url" required> |
| 24 | + |
| 25 | + <label for="image_description"><?php _e( 'Image Description', 'quick-weblog' ); ?></label> |
| 26 | + <textarea name="image_description" id="image_description" required></textarea> |
| 27 | + |
| 28 | + <label for="quote"><?php _e( 'Quote', 'quick-weblog' ); ?></label> |
| 29 | + <textarea name="quote" id="quote" required></textarea> |
| 30 | + |
| 31 | + <label for="url"><?php _e( 'URL', 'quick-weblog' ); ?></label> |
| 32 | + <input type="text" name="url" id="url" required> |
| 33 | + |
| 34 | + <label for="category"><?php _e( 'Category', 'quick-weblog' ); ?></label> |
| 35 | + <?php wp_dropdown_categories( array( 'name' => 'category', 'orderby' => 'name', 'taxonomy' => 'category' ) ); ?> |
| 36 | + |
| 37 | + <label for="tags"><?php _e( 'Tags', 'quick-weblog' ); ?></label> |
| 38 | + <input type="text" name="tags" id="tags" required> |
| 39 | + |
| 40 | + <input type="hidden" name="action" value="quick_weblog_submit_form"> |
| 41 | + <?php wp_nonce_field( 'quick_weblog_submit_form', 'quick_weblog_form_nonce' ); ?> |
| 42 | + <input type="submit" value="<?php _e( 'Submit', 'quick-weblog' ); ?>"> |
| 43 | + </form> |
| 44 | + <?php |
| 45 | +} |
| 46 | + |
| 47 | +function quick_weblog_add_form_to_page() { |
| 48 | + add_shortcode( 'quick-weblog-form', 'quick_weblog_form' ); |
| 49 | +} |
| 50 | +add_action( 'init', 'quick_weblog_add_form_to_page' ); |
| 51 | + |
| 52 | +function quick_weblog_add_menu_page() { |
| 53 | + add_menu_page( |
| 54 | + __( 'Quick Weblog', 'quick-weblog' ), // Page title |
| 55 | + __( 'Quick Weblog', 'quick-weblog' ), // Menu title |
| 56 | + 'manage_options', // Capability required to access the page |
| 57 | + 'quick-weblog', // Menu slug |
| 58 | + 'quick_weblog_menu_page', // Callback function to render the page |
| 59 | + 'dashicons-admin-post', // Icon |
| 60 | + 30 // Position in the menu |
| 61 | + ); |
| 62 | +} |
| 63 | +add_action( 'admin_menu', 'quick_weblog_add_menu_page' ); |
| 64 | + |
| 65 | +function quick_weblog_menu_page() { |
| 66 | + ?> |
| 67 | + <div class="wrap"> |
| 68 | + <h1><?php _e( 'Quick Weblog', 'quick-weblog' ); ?></h1> |
| 69 | + <?php quick_weblog_form(); ?> |
| 70 | + </div> |
| 71 | + <?php |
| 72 | +} |
| 73 | + |
| 74 | +function quick_weblog_submit_form() { |
| 75 | + // Check the nonce to verify the form submission |
| 76 | + if ( ! isset( $_POST['quick_weblog_form_nonce'] ) || ! wp_verify_nonce( $_POST['quick_weblog_form_nonce'], 'quick_weblog_submit_form' ) ) { |
| 77 | + wp_die( __( 'Error: Invalid nonce.', 'quick-weblog' ) ); |
| 78 | + } |
| 79 | + |
| 80 | + // Get the form data |
| 81 | + $title = sanitize_text_field( $_POST['title'] ); |
| 82 | + $image_url = sanitize_text_field( $_POST['image_url'] ); |
| 83 | + $image_description = sanitize_text_field( $_POST['image_description'] ); |
| 84 | + $quote = sanitize_text_field( $_POST['quote'] ); |
| 85 | + $url = esc_url_raw( $_POST['url'] ); |
| 86 | + $category = intval( $_POST['category'] ); |
| 87 | + $tags = sanitize_text_field( $_POST['tags'] ); |
| 88 | + |
| 89 | + // Create a new post |
| 90 | + $post_data = array( |
| 91 | + 'post_title' => $title, |
| 92 | + 'post_content' => sprintf( '<blockquote>%s</blockquote><img src="%s" alt="%s"><p><a href="%s" target="_blank">%s</a></p>', $quote, $image_url, $image_description, $url, $url ), |
| 93 | + 'post_category' => array( $category ), |
| 94 | + 'tags_input' => $tags, |
| 95 | + 'post_status' => 'publish' |
| 96 | + ); |
| 97 | + $post_id = wp_insert_post( $post_data ); |
| 98 | + |
| 99 | + // Redirect to the new post |
| 100 | + wp_redirect( get_permalink( $post_id ) ); |
| 101 | + exit(); |
| 102 | +} |
| 103 | +add_action( 'admin_post_quick_weblog_submit_form', 'quick_weblog_submit_form' ); |
| 104 | + |
| 105 | +?> |
0 commit comments