My Custom CSS PHP & ADS Shortcodes

Below, you’ll find a detailed guide on how to add the My Custom CSS PHP & ADS Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the My Custom CSS PHP & ADS Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the My Custom CSS PHP & ADS Plugin and the shortcodes it provides:

Plugin Icon
My Custom CSS PHP & ADS

"My Custom CSS PHP & ADS is a versatile WordPress plugin that allows users to easily customize their site's appearance with CSS, PHP, and ADS. It's user-friendly and a great tool for non-coders."

★★★★✩ (42) Active Installs: 5000+ Tested with: 5.0.20 PHP Version: false
Included Shortcodes:
  • [my_custom_ad]
  • [MyPHPCode]
  • [my_custom_php]

[my_custom_ad] Shortcode

The ‘my_custom_ad’ shortcode from My-Custom-CSS plugin renders custom ads. It fetches a specific ad snippet from the database based on the provided ‘id’. . If the ‘id’ is invalid or the ad snippet is inactive, it returns an empty string. Only ad snippets with ‘ad_shortcode’ as code_type are processed. The code is then filtered and returned.

Shortcode: [my_custom_ad]

Parameters

Here is a list of all possible my_custom_ad shortcode parameters and attributes:

  • id – The unique identifier for the specific ad snippet

Examples and Usage

Basic example – Use the shortcode to display a custom ad by referencing its unique ID.

[my_custom_ad id=1 /]

Advanced examples

Use the shortcode to display multiple custom ads by using their unique IDs. Please note that the ads will display in the order of the IDs.

[my_custom_ad id=1 /]
[my_custom_ad id=2 /]
[my_custom_ad id=3 /]

Use the shortcode to display a custom ad by referencing its unique ID within a post or page content. This will allow the ad to be embedded within your content, providing a seamless user experience.

<div>Here is an interesting ad for you: [my_custom_ad id=1 /]</div>

Please note that for all the above examples, you need to replace the number after ‘id=’ with the actual ID of the ad you want to display.

PHP Function Code

In case you have difficulties debugging what causing issues with [my_custom_ad] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('my_custom_ad', array($this, 'render'));

Shortcode PHP function:

function render($atts){
        $atts = shortcode_atts(
            array(
                'id'      => 0,
            ),
            $atts, 'my_custom_ad'
        );

        if ( ! $id = intval( $atts['id'] ) ) {
            return '';
        }

        $snippet = DatabaseManager::getInstance()->getSnippetByID( $id );
        $code = $snippet->code;
        $blockIndex = 0;
        $totalBlocks = 1;
        if($snippet->code_type !== 'ad_shortcode'){
            return ''; // todo messages
        }
        if ( ! trim( $snippet->code ) or !$snippet->active) {
            return '';
        }
        $code = apply_filters('myccad/frontend/ad_code', $code, $snippet->id, $blockIndex, $totalBlocks);

        return $code;
    }

Code file location:

my-custom-css/my-custom-css/behaviors/AdShortcodeBehavior.php

[MyPHPCode] Shortcode

The MyPHPCode shortcode is a custom plugin that renders PHP code snippets on a WordPress site. It accepts two parameters – ‘id’ and ‘network’. ‘id’ refers to the unique identifier of the PHP code snippet to be displayed. If no ‘id’ is provided or if the ‘id’ doesn’t correspond to any snippet, the shortcode returns an empty string. The ‘network’ parameter is used to toggle between network-specific and site-specific snippets. If ‘network’ is set to true, it fetches network-specific snippets, otherwise it fetches site-specific snippets. The output of the shortcode is a stylized code block containing the PHP code snippet.

Shortcode: [MyPHPCode]

Parameters

Here is a list of all possible MyPHPCode shortcode parameters and attributes:

  • id – A unique number that identifies the snippet
  • network – Determines if the snippet is a network-wide snippet

Examples and Usage

Basic example – Showcases the use of the ‘MyPHPCode’ shortcode by referencing the ID parameter.

[MyPHPCode id=3 /]

Advanced examples

Example 1 – Demonstrates the use of the ‘MyPHPCode’ shortcode by referencing both ID and network parameters. The code will first try to load by ID, but if not found, it will try to load by network.

[MyPHPCode id=3 network=true /]

Example 2 – Illustrates the use of the ‘MyPHPCode’ shortcode without any parameters. In this case, the function will return an empty string due to the absence of ID.

[MyPHPCode /]

Example 3 – Exhibits the use of the ‘MyPHPCode’ shortcode with a non-integer ID. The function will return an empty string as it cannot convert the ID to an integer.

[MyPHPCode id="a" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [MyPHPCode] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'MyPHPCode', array( $this, 'render_shortcode' ) );

Shortcode PHP function:

function render_shortcode( $atts ) {

		$atts = shortcode_atts(
			array(
				'id'      => 0,
				'network' => false,
			),
			$atts, 'MyPHPCode'
		);

		if ( ! $id = intval( $atts['id'] ) ) {
			return '';
		}

		$network = $atts['network'] ? true : false;
		$snippet = get_snippet( $id, $network );

		if ( ! trim( $snippet->code ) ) {
			return '';
		}

		return '<pre><code class="language-php">' . esc_html( $snippet->code ) . '</code></pre>';
	}

Code file location:

my-custom-css/my-custom-css/modules/custom-php/php/class-shortcode.php

[my_custom_php] Shortcode

The My Custom CSS plugin shortcode enables the execution of PHP snippets within WordPress posts. This shortcode triggers the ‘phpShortcodeRender’ function, which first checks if safe mode is active. If it is, the function returns false, preventing the snippet from running. If safe mode is not active, the function processes the attributes ‘id’ and ‘network’. The ‘id’ attribute corresponds to the specific PHP snippet to be executed. If this is not present or valid, the function returns an empty string. The ‘network’ attribute determines whether the snippet should be executed across a network. The function then retrieves the snippet based on the ‘id’ and ‘network’ attributes. If the snippet is not active or contains no code, the function again returns an empty string. If the snippet is active and contains code, the function executes the code and captures the output. This output is then returned as HTML.

Shortcode: [my_custom_php]

Parameters

Here is a list of all possible my_custom_php shortcode parameters and attributes:

  • id – The unique identifier for the specific snippet.
  • network – Indicates if the snippet is network-wide or not.

Examples and Usage

Basic example – A simple use of the ‘my_custom_php’ shortcode to execute a PHP snippet with a specified ID.

[my_custom_php id=1 /]

Advanced examples

Using the ‘my_custom_php’ shortcode to execute a PHP snippet with a specified ID, and specifying whether to use the network-wide setting or not. If the ‘network’ parameter is set to true, the snippet will be executed across all sites on a WordPress multisite network.

[my_custom_php id=1 network=true /]

Using the ‘my_custom_php’ shortcode to execute a PHP snippet with a specified ID, and specifying whether to use the network-wide setting or not. If the ‘network’ parameter is set to false, the snippet will only be executed on the current site.

[my_custom_php id=1 network=false /]

PHP Function Code

In case you have difficulties debugging what causing issues with [my_custom_php] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('my_custom_php', array($this, 'phpShortcodeRender'));

Shortcode PHP function:

function phpShortcodeRender($atts)
    {
        /* Bail early if safe mode is active */
        if ( defined( 'MY_CUSTOM_PHP_SAFE_MODE' ) && MY_CUSTOM_PHP_SAFE_MODE || ! apply_filters( 'custom_php/execute_snippets', true ) ) {
            return false;
        }

        $atts = shortcode_atts(
            array(
                'id'      => 0,
                'network' => false,
            ),
            $atts, 'MyPHPCode'
        );

        if ( ! $id = intval( $atts['id'] ) ) {
            return '';
        }

        $network = $atts['network'] ? true : false;
        $snippet = get_snippet( $id, $network );

        if ( ! trim( $snippet->code ) or !$snippet->active) {
            return '';
        }

        ob_start();

        eval( $snippet->code );

        $html = ob_get_clean();
        return $html;
    }

Code file location:

my-custom-css/my-custom-css/modules/custom-php/php/class-shortcode.php

Conclusion

Now that you’ve learned how to embed the My Custom CSS PHP & ADS Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *