Ask your WordPress questions! Pay money and get answers fast! (more info)

use a MagicField function in my functions.php WordPress

  • SOLVED

In functions.php I wrote a function to create a metabox.
In this metabox I have a SELECT populated by datas coming from MF fields. I retrieve the values for this SELECT from a MagicField duplicate group (lieu_et_horaire).
For each group I retrieve three values ($id_lieu, $salle, $ville).



function slh_get_sample_options() {
$options = array (
'sélectionnez un cours' => 'sélectionnez un cours',
'stages' => 'tous les stages'
);

global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'cours',
'post_status' => 'publish'
);
$tous_les_cours = get_post($args);

foreach ($tous_les_cours as $cours) {

$lieux_horaires = get_group('lieu_et_horaire',$cours->ID);

if($lieux_horaires) {
$tablo_sorted = array();
foreach ($lieux_horaires as $lieu_horaire) {
$id_lieu = $lieu_horaire['lieu_et_horaire_lieu_du_cours'][1]?$lieu_horaire['lieu_et_horaire_lieu_du_cours'][1] : '';
$salle = get('coordonnees_nom_du_lieu',1,1,$id_lieu)?get('coordonnees_nom_du_lieu',1,1,$id_lieu) : '';
$ville = get('coordonnees_ville',1,1,$id_lieu) ? ucwords(get('coordonnees_ville',1,1,$id_lieu)) : 'Paris';

$options = $id_lieu.', '.$salle.', '.$ville;
}



}
}


return $options;
}

add_action('admin_init', 'slh_add_meta_boxes', 1);




In my function I need use some MF functions like get_group but I have this error message

Fatal error: Call to undefined function get_group() in /www/sites_clients/spectacles/wp-content/themes/theatre/functions.php on line 27

Why can't I use a function like get_group in my function ?
how can I use a function like get_group in my function or how can I retrieve in my function the values inserted in my MF fields ?

Answers (1)

2014-04-29

Dbranes answers:

The function definition of <em>get_group()</em> is only loaded for the frontend.

You could try to copy the function definition, with a prefix, and add it to your <em>functions.php</em> file or try this:


/**
* Add a support for get_group() in the backend
* @link http://www.wpquestions.com/question/showChrono/id/9537
*/

function wpq_9537_admin_init() {

// Edit to your needs:
$file = WP_PLUGIN_DIR . '/magic-fields-2/mf_front_end.php';

if ( file_exists( $file ) ) {
require_once( $file );
}
}

add_action( 'admin_init', 'wpq_9537_admin_init' );


Sébastien | French WordpressDesigner comments:

Sorry Dbranes but I have already resolved this problem.
My solution was to add the function get_group with a prefix ! :)


Dbranes comments:

ok, good ;-)

Since the <em>get_group()</em> function depends on the following functions:

_processed_value()
aux_image()
_processed_params()


I guess you had to copy them too with a prefix?

That's why I suggested the second method, to save work ;-)

cheers


Sébastien | French WordpressDesigner comments:

yes i copied them too :)
But your second method is more pretty !
I copy your method and gives you the award :)