Simple Category Enhancements 1.0

Prosta wtyczka wordpressowa sklejona na szybko na użytek tej strony. Publikuję ją tutaj dla testu wrzucania kodu źródłowego. Wtyczka dodaje do stron kategorii postów listę kategorii podrzędnych oraz odnośnik do kanału RSS dla danej kategorii.

Widok ustawień wtyczki w menu dostosowania motywu wordpressa. Simple Category Enhancements
Widok ustawień wtyczki w menu dostosowania.
PHP
<?php
    /**
    * Plugin Name: Simple Category Enhancements by DM
    * Description: Minimalist WordPress plugin that adds simple enhancements to category page.
    * Version: 1.0
    * Author: Dawid Mikulski
    */

    /*
        Creating simple settings page for:
        - Subcategory list header
        - RSS text
    */

    // Add settings to the Customizer.
    function sce_customize_register($wp_customize) {
        // Section for plugin settings.
        $wp_customize->add_section('sce_settings_section', array(
            'title' => __('Simple Category Enhancements', 'sce'),
            'priority' => 30,
        ));

        // Setting for subcategory heading text.
        $wp_customize->add_setting('sce_subcategory_heading_text', array(
            'default' => __('Subcategories:', 'sce'),
            'sanitize_callback' => 'sanitize_text_field',
        ));

        $wp_customize->add_control('sce_subcategory_heading_text_control', array(
            'label' => __('Subcategory Heading Text', 'sce'),
            'section' => 'sce_settings_section',
            'settings' => 'sce_subcategory_heading_text',
            'type' => 'text',
        ));

        // Setting for RSS link text.
        $wp_customize->add_setting('sce_rss_link_text', array(
            'default' => __('Subscribe to RSS', 'sce'),
            'sanitize_callback' => 'sanitize_text_field',
        ));

        $wp_customize->add_control('sce_rss_link_text_control', array(
            'label' => __('RSS Link Text', 'sce'),
            'section' => 'sce_settings_section',
            'settings' => 'sce_rss_link_text',
            'type' => 'text',
        ));
    }

    /*
        Displaying list of subcategories under category
        description at category page.
    */
    
    function sce_display_subcategories_list($description) {
        if (is_category()) {
            // Get the current category.
            $current_category = get_queried_object();

            // Get subcategories.
            $subcategories = get_categories(array(
                'child_of' => $current_category->term_id,
                'hide_empty' => false,
            ));

            // Check if there are subcategories.
            if (!empty($subcategories)) {
                $heading_text = get_theme_mod('sce_subcategory_heading_text', __('Subcategories:', 'sce'));
                $output = '<div class="subcategory-list">';
                $output .= '<h3>' . esc_html($heading_text) . '</h3>';
                $output .= '<ul>';

                // Display each subcategory as a list item.
                foreach ($subcategories as $subcategory) {
                    $output .= '<li><a href="' . get_category_link($subcategory->term_id) . '">' . $subcategory->name . '</a></li>';
                }

                $output .= '</ul>';
                $output .= '</div>';

                // Append the subcategory list to the description.
                $description .= $output;
            }
        }
        return $description;
    }

    /*
        Creating 'a' element with RSS URL for current category
    */

    function sce_add_rss_icon($description) {
        if (is_category()) {
            $current_category = get_queried_object();
            $rss_url = get_category_feed_link($current_category->term_id);
            $rss_text = get_theme_mod('sce_rss_link_text', __('Subscribe to RSS', 'sce'));

            $icon = '<form action="'.esc_url($rss_url).'" method="get" target="_blank">
            <button type="submit"><span>
            <svg width="20" height="20" viewBox="0 0 20 20" aria-hidden="true">
            <path d="M17.9,0H2.1C1,0,0,1,0,2.1v15.7C0,19,1,20,2.1,20h15.7c1.2,0,2.1-1,2.1-2.1V2.1C20,1,19,0,17.9,0z M5,17.1c-1.2,0-2.1-1-2.1-2.1s1-2.1,2.1-2.1s2.1,1,2.1,2.1S6.2,17.1,5,17.1z M12,17.1h-1.5c-0.3,0-0.5-0.2-0.5-0.5c-0.2-3.6-3.1-6.4-6.7-6.7c-0.3,0-0.5-0.2-0.5-0.5V8c0-0.3,0.2-0.5,0.5-0.5c4.9,0.3,8.9,4.2,9.2,9.2C12.6,16.9,12.3,17.1,12,17.1L12,17.1z M16.6,17.1h-1.5c-0.3,0-0.5-0.2-0.5-0.5c-0.2-6.1-5.1-11-11.2-11.2c-0.3,0-0.5-0.2-0.5-0.5V3.4c0-0.3,0.2-0.5,0.5-0.5c7.5,0.3,13.5,6.3,13.8,13.8C17.2,16.9,16.9,17.1,16.6,17.1L16.6,17.1z"></path></svg></span> '.esc_attr($rss_text).'</button>
            </form>';

            $description .= $icon;
        }
        return $description;
    }

    /*
        Add functions
    */
    add_action('customize_register', 'sce_customize_register');
    add_filter('category_description', 'sce_add_rss_icon');
    add_filter('category_description', 'sce_display_subcategories_list');

?>
Rozwiń

Simple Category Enhancements

Wersja: 1.0.0

Source

Pobierz

Windows

Pobierz

Linux

Pobierz

MacOS

Pobierz

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Przejdź do treści