Search code examples
phpwordpressfunctionjet-engine

i am creating a function for jet engine to display QR in the post


I want to be able to display a Qr code dynamically created with the post url, i'm using jet engine qr module, i saw the code in a video but i'snt working for me this is the function code that i'm trying:

also i want to add this function using code snippets.

add_filer( 'jet-engine/listings/allowed-callbacks', 'add_custom_dynamic_field_callback' );

add_filter( 'jet-engine/listing/dynamic-field/callback-args', 'add_custom_dynamic_field_callbacks_args', 0, 3);

add_action ('jet-engine/listing/dynamic-field/callback-controlls' , 'add_custom_control', 0, 1 );


function add_custom_dynamic_field_callbacks( $callbacks) {
    $callbacks['url_as_qr'] = 'Post URL as Qr-code';
    return $callback;
}

function url_as_qr( $value, $code_size) {
    if ( class_exists( 'Jet_Engine_Module_QR_Code') ) {
        $qr = new Jet_Engine_Module_QR_Code();
        $result = $qr->get_qr_code( $value, $code_size['size'] );
    }
    return $result;
}

function add_custom_dynamic_field_callbacks_args( $result, $callback, $settings) {
    if ($callback === 'url_as_qr') {
        $result = array( get_permalink(), $settings['code_size'] );
    }
    return $result;
}
    
function add_cutom_control( $controls ) {
    
    $controls->add_control(
        'code_size' ,
        array(
            'label' => 'QR_Code_Size',
            'type'  => 'slider',
            'range' => array(
                'px' => array(
                    'nis' => 50,
                    'nax' => 400,
                ),
            ),
            'condition' => array(
                'dynamic_field_filter' => 'yes'
                'filter_callback'      => array( 'url_as_qr'),
            ),
        )
    );
}               

Solution

  • There are some typos in your code that may cause the issues.

    1. In the first line: use add_filter instead of add_filer
    2. Typo in the add_custom_dynamic_field_callbacks function: you are returning $callback instead of $callbacks
    3. Typo in the add_custom_control function: your code says add_cutom_control instead of add_custom_control
    4. There is a missing comma in the 'condition' array after 'yes'
    5. Typo in the 'range' array: It should be min and max not nis and nax
    6. Incorrect action hook: In the third line the hook should be jet-engine/listing/dynamic-field/callback-controlls (callback-controlls instead of callback-controls).

    The corrected code looks like this:

    add_filter( 'jet-engine/listings/allowed-callbacks', 'add_custom_dynamic_field_callbacks' );
    add_filter( 'jet-engine/listing/dynamic-field/callback-args', 'add_custom_dynamic_field_callbacks_args', 0, 3 );
    add_action( 'jet-engine/listing/dynamic-field/callback-controls', 'add_custom_control', 0, 1 );
    
    function add_custom_dynamic_field_callbacks( $callbacks ) {
        $callbacks['url_as_qr'] = 'Post URL as QR-code';
        return $callbacks;
    }
    
    function url_as_qr( $value, $code_size ) {
        if ( class_exists( 'Jet_Engine_Module_QR_Code' ) ) {
            $qr = new Jet_Engine_Module_QR_Code();
            $result = $qr->get_qr_code( $value, $code_size['size'] );
        }
        return $result;
    }
    
    function add_custom_dynamic_field_callbacks_args( $result, $callback, $settings ) {
        if ( $callback === 'url_as_qr' ) {
            $result = array( get_permalink(), $settings['code_size'] );
        }
        return $result;
    }
    
    function add_custom_control( $controls ) {
        $controls->add_control(
            'code_size',
            array(
                'label' => 'QR_Code_Size',
                'type' => 'slider',
                'range' => array(
                    'px' => array(
                        'min' => 50,
                        'max' => 400,
                    ),
                ),
                'condition' => array(
                    'dynamic_field_filter' => 'yes',
                    'filter_callback' => array( 'url_as_qr' ),
                ),
            )
        );
    }
    

    I did not test this code, maybe you could add some error messages to your question if this solution doesn't solve your problem.