Author: milos

  • 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.

  • Kreiranje prilagođenih postova (CPT – Custom Post Types DOKER)

    ✅ Dodavanje novog product_review posta

    docker compose -f docker/docker-compose.yml exec wordpress wp post create \ --post_type=product_review \ --post_title="Test Review" \ --post_status=publish \ --allow-root

    ✅ Prikaz svih product_review postova

    docker compose -f docker/docker-compose.yml exec wordpress wp post list \ --post_type=product_review \ --allow-root

    ✅ Testiranje REST API-ja (GET)

    curl -X GET http://localhost:8080/wp-json/mock-api/v1/review-summary/

    ✅ Testiranje REST API-ja (POST)

    curl -X POST http://localhost:8080/wp-json/mock-api/v1/sentiment/ \ -H "Content-Type: application/json" \ -d '{"text": "This is an amazing product!"}'

    ✅ Testiranje sa httpie (ako je instaliran)

    http GET http://localhost:8080/wp-json/mock-api/v1/review-summary/

    ✅ Debug WordPress okruženja (prikaz URL‑a sajta)

    docker compose -f docker/docker-compose.yml exec wordpress wp option get siteurl --allow-root

    ✅ Brzo testiranje PHP‑a u WP CLI‑u

    docker compose -f docker/docker-compose.yml exec wordpress wp eval 'echo get_bloginfo("name");' --allow-root
  • WordPress WP-CLI komande koje ćeš često koristiti

    wp plugin list --allow-rootPrikazuje listu aktivnih i instaliranih pluginova
    wp theme list --allow-rootPrikazuje listu tema
    wp post list --post_type=product_review --allow-rootPrikazuje sve recenzije (product_review CPT)
    wp post create --post_type=product_review --post_title="Example" --post_status=publish --allow-rootKreira novu recenziju
    wp option get siteurl --allow-rootPrikazuje URL sajta
    wp user list --allow-rootPrikazuje sve korisnike
    wp eval 'echo get_bloginfo("name");' --allow-root



    MySQL iz CLI

    Ako želiš da pristupiš bazi direktno:

    docker compose -f docker/docker-compose.yml exec db mysql -uroot -proot wordpress