Alexandru Andrei Costache Alexandru Andrei Costache - 1 month ago 7
PHP Question

SMOF transition to the Redux Framework

I try to make the transfer of a WordPress theme options from SMOF to the Redux Framework since SMOF is no longer maintained. I have successfully transferred all other options and they work perfectly but I have trouble in following context, getting this error:

Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'


The old code was used to create a built-in translator option using
"type" => "text"
, and has works great but no I do not know how to implement it in the new Redux Framework to run and no longer occur this error.

Here's the old code from the SMOF Framework:

$translate_strings = theme_get_translate_options();

foreach ( $translate_strings as $string_key => $string ) {

$of_options[] = array( "name" => esc_html( $string['string_text'] ),
"id" => 'td_'.$string_key,
"type" => "text",
);
}


And here is the new interface of Redux Framework. where are supposed to implement the old code using :
"type" => "text",
:

$translate_strings = theme_get_translate_options();

// -> START Translation Info Fields
Redux::setSection( $opt_name, array(
'title' => __( 'Translator', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
'id' => 'translator-info-subsection',
'subsection' => true,
'fields' => array(

array(
'id' => '',
'type' => 'text',
'title' => __( '', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
),

)
) );


The code that I used and makes the errors occurs is the following:

$translate_strings = theme_get_translate_options();

Redux::setSection( $opt_name, array(
'title' => __( 'Translator', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
'id' => 'translator-info-subsection',
'subsection' => true,
'fields' => array(

foreach ( $translate_strings as $string_key => $string ) {
array(
'id' => 'td_'.$string_key,
'type' => 'text',
'title' => __( '', 'redux-framework-demo' ),
),
}

)
) );


I'm completely stuck on this and any help or suggestion will be greatly appreciated.

Thank you!

Answer

A little reorganization should be enough:

$translate_strings = theme_get_translate_options();
$myFields = array();

        foreach ( $translate_strings as $string_key => $string ) {
            $myFields[]=
            array(
                'id'       => 'td_'.$string_key,
                'type'     => 'text',
                'title'    => __( '', 'redux-framework-demo' ),
                );
        }
Redux::setSection( $opt_name, array(
    'title'      => __( 'Translator', 'redux-framework-demo' ),
    'desc'       => __( '', 'redux-framework-demo' ),
    'id'         => 'translator-info-subsection',
    'subsection' => true,
    'fields'     => $myFields
    ) );

I took out your foreach from the initialization and initialized the array separately.

Comments