addSettingSections( $this->sPageSlug, // the target page slug array( 'tab_slug' => $this->sTabSlug, 'section_id' => $this->sSectionID, 'title' => __( 'Using Callbacks', 'admin-page-framework-loader' ), 'description' => __( 'These fields are (re)defined with callbacks.', 'admin-page-framework-loader' ), ) ); // Fields $oFactory->addSettingFields( $this->sSectionID, // the target section ID array( 'field_id' => 'callback_example', 'type' => 'select', ), array( 'field_id' => 'apf_post_titles', 'type' => 'checkbox', 'label_min_width' => '100%', ) ); add_filter( 'field_definition_' . $oFactory->oProp->sClassName . '_' . $this->sSectionID . '_callback_example', array( $this, 'replyToRedefineExampleField' ) ); add_filter( 'field_definition_' . $oFactory->oProp->sClassName . '_' . $this->sSectionID . '_apf_post_titles', array( $this, 'replyToRedefinePostTitleField' ) ); } /** * Field callback methods - for field definitions that require heavy tasks should be defined with the callback methods. * * @callback filter field_definition_{instantiated class name}_{section id}_{field_id} * @return array */ public function replyToRedefineExampleField( $aField ) { $aField[ 'title' ] = __( 'Post Titles', 'admin-page-framework-loader' ); $aField[ 'description' ] = sprintf( __( 'This description is inserted with the filter, named: %1$s.', 'admin-page-framework-loader' ), current_filter() ); $aField[ 'label' ] = $this->_getPostTitles(); return $aField; } /** * Field callback methods - for field definitions that require heavy tasks should be defined with the callback methods. * * @callback filter field_definition_{instantiated class name}_{section id}_{field_id} * @return array */ public function replyToRedefinePostTitleField( $aField ) { // field_definition_{instantiated class name}_{section id}_{field_id} $aField[ 'title' ] = __( 'APF Custom Post Titles', 'admin-page-framework-loader' ); $aField[ 'label' ] = $this->_getPostTitles( AdminPageFrameworkLoader_Registry::$aPostTypes[ 'demo' ] ); return $aField; } /** * @return array */ private function _getPostTitles( $sPostTypeSlug='post' ) { $_aArgs = array( 'post_type' => $sPostTypeSlug, ); $_oResults = new WP_Query( $_aArgs ); $_aPostTitles = array(); foreach( $_oResults->posts as $_iIndex => $_oPost ) { $_aPostTitles[ $_oPost->ID ] = $_oPost->post_title; } return $_aPostTitles; } }