Support » Allgemeine Fragen » Create new class object inside WordPress template

  • I’ve a problem. My plan is it to create a new class object inside a WordPress template to pass a variable from the post object to it:

    <?php
    /**
     * Template Name: Ticket
     */
    
    defined( 'ABSPATH' ) || exit;
    
    new Test( $post->ID );

    Inside my class I’m defining an action that adds a AJAX function inside the class:

    class Test {
    	public function __construct( $ticket_id ) {
    		$this->ticket_id = $ticket_id;
    
    		$this->register();
    	}
    
    	/**
    	 * Register all hooks
    	 */
    	public function register(): void {
    		add_action( 'wp_ajax_test', array( $this, 'test' ) );
    	}
    
    	public function test(): void {
    		error_log( $this->ticket_id );
    	}
    }

    The problem is that the AJAX function is not reachable . Maybe it’s added too late? Because when I create a new class object directly in my functions.php file, it’s working . In this case I don’t know how to get my post id inside my class.

    Thanks for helping me out!

Ansicht von 1 Antwort (von insgesamt 1)
  • Moderation Note: I close this thread as you have asked your question in a German support forum in English. To keep the threads accessible to all people not speaking English, I kindly ask you to either rephrase your question in German or ask in the International support forums. Thank you for your understanding. Best regards, Bego

Ansicht von 1 Antwort (von insgesamt 1)
  • Das Thema „Create new class object inside WordPress template“ ist für neue Antworten geschlossen.