OSNOVNE FUNKCIJE

✅ 1. Registracija Custom Post Type (CPT)

add_action('init', function() {
    register_post_type('book', [
        'labels' => [
            'name'          => 'Books',
            'singular_name' => 'Book'
        ],
        'public'        => true,
        'has_archive'   => true,
        'show_in_rest'  => true,
        'supports'      => ['title', 'editor', 'thumbnail'],
    ]);
});

➡️ Ovo pravi Books post type koji vidiš u WP Admin-u.

✅ 2. Registracija Custom Taxonomy

add_action('init', function() {
    register_taxonomy('genre', 'book', [
        'label'        => 'Genres',
        'rewrite'      => ['slug' => 'genre'],
        'hierarchical' => true,
        'show_in_rest' => true,
    ]);
});

➡️ Pravi Genres kategorije koje su povezane sa post type-om book.

✅ 3. Registracija Shortcode-a

add_shortcode('hello', function() {
    return 'Hello, this is a custom shortcode!';
});

➡️ Kada u editor napišeš [hello], prikazuje Hello, this is a custom shortcode!

✅ 4. Dodavanje Custom User Role

add_action('init', function() {
    add_role('custom_role', 'Custom Role', [
        'read' => true,
        'edit_posts' => false,
    ]);
});

➡️ Dodaje novu rolu “Custom Role” sa ograničenim pravima.

✅ 5. Dodavanje novog User-a iz PHP-a

wp_create_user('newuser', 'securepassword', 'newuser@example.com');

➡️ Možeš pozvati u functions.php (ali samo privremeno, jer se izvršava prilikom svakog load-a stranice dok ne ukloniš).

✅ 6. Dodavanje Custom REST API Endpoint-a

add_action('rest_api_init', function() {
    register_rest_route('custom/v1', '/hello', [
        'methods'  => 'GET',
        'callback' => function() {
            return ['message' => 'Hello from REST API'];
        },
    ]);
});

➡️ Pristupaš ovome na: http://example.com/wp-json/custom/v1/hello

✅ 7. Custom Meta Field za CPT

add_action('init', function() {
    register_post_meta('book', 'rating', [
        'type'         => 'number',
        'single'       => true,
        'show_in_rest' => true,
    ]);
});

➡️ Dodaješ dodatno polje rating za Books post type.

✅ 8. Hook-ovi koje stalno koristiš

init               // Registracija CPT, taxonomy, shortcodes
rest_api_init      // Dodavanje REST endpointa
wp_enqueue_scripts // Učitavanje CSS/JS
admin_menu         // Dodavanje admin menija
the_content        // Filter za sadržaj posta

✅ 9. Prikaz CPT postova u template-u

$books = new WP_Query(['post_type' => 'book']);
if ($books->have_posts()) {
    while ($books->have_posts()) {
        $books->the_post();
        echo '<h2>' . get_the_title() . '</h2>';
    }
}
wp_reset_postdata();

✅ 10. Kreiranje Page template-a

<?php
/**
 * Template Name: Custom Page
 */
get_header();
echo '<h1>This is a custom page template</h1>';
get_footer();

➡️ Sačuvaj kao page-custom.php.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *