How do I add attributes support to a custom model

The class-attribute.php is located in wp-contents/plugins/hivepress/includes/components

All I want to do is to dynamically change this:

<?php
*
 * Attribute component.
 *
 * @package HivePress\Components
 */

namespace HivePress\Components;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Handles model attributes.
 */
final class Attribute extends Component {

	/**
	 * Model parameters.
	 *
	 * @var array
	 */
	protected $models = [];

	/**
	 * Model attributes.
	 *
	 * @var array
	 */
	protected $attributes = [];

	/**
	 * Class constructor.
	 *
	 * @param array $args Component arguments.
	 */
	public function __construct( $args = [] ) {
		$args = hp\merge_arrays(
			[
				'models' => [
					'listing' => [],
					'vendor'  => [],
					'user'    => [],
				],
			],
			$args
		);

		parent::__construct( $args );
	}

TO THIS:

<?php
/**
 * Attribute component.
 *
 * @package HivePress\Components
 */

namespace HivePress\Components;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/*
 * Handles model attributes.
 */
final class Attribute extends Component {

	/**
	 * Model parameters.
	 *
	 * @var array
	 */
	protected $models = [];

	/**
	 * Model attributes.
	 *
	 * @var array
	 */
	protected $attributes = [];

	/**
	 * Class constructor.
	 *
	 * @param array $args Component arguments.
	 */
	public function __construct( $args = [] ) {
		$args = hp\merge_arrays(
			[
				'models' => [
					'listing' => [],
					'vendor'  => [],
					'user'    => [],
                                        'escort'    => [],
				],
			],
			$args
		);

		parent::__construct( $args );
	}

USING a filter but, those listed at Source: components/class-attribute.php | HivePress Hook Reference doesn’t seem to work.

Here’s my code (trial A):

<?php
/**
 * Attribute component.
 *
 * @package HivePress\Components
 */

namespace HivePress\Components;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Handles model attributes.
 */
add_filter(
    'hivepress/v1/components/attribute/models',
    function( $models ) {
        // Add 'escort' post type to the attribute-enabled models.
        $models[] = 'escort';

        return $models;
    }
);

TRIAL B:

<?php
/**
 * Attribute component.
 *
 * @package HivePress\Components
 */

namespace HivePress\Components;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Handles model attributes.
 */
class Attribute extends Component {

    /**
     * Class constructor.
     *
     * @param array $args Component arguments.
     */
    public function __construct( $args = [] ) {
        parent::__construct( $args );

        add_filter(
            'hivepress/v1/components/attribute/models',
            [ $this, 'add_escort_model' ]
        );
    }

    /**
     * Adds the 'escort' post type to the attribute-enabled models.
     *
     * @param array $models Model names.
     * @return array
     */
    public function add_escort_model( $models ) {
        // Add 'escort' post type to the attribute-enabled models.
        $models[] = 'escort';

        return $models;
    }
}

ALL I WANT IS TO INTEGRATE MY CUSTOM POST TYPE ESCORT WITH THE EXISTING HIVEPRESS ATTRIBUTES

Pls help!

You don’t have to extend the class itself, please try using the existing hooks to insert a new model name to a list of existing models. Then you can add attributes via the hook to this new model, but the model class must exist so you have to implement it via a custom extension. Please check how the vendor or listing model class is implemented.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.