0
votes

New question about the same problem. Trying to use a php stylesheet with custom wordpress setting in it. But once I add

$myoptions = get_option( 'option_name' );

into this header

<?php
header("Content-type: text/css; charset: UTF-8");
$myoptions = get_option( 'custom_ads' );
?>

stylesheet goes error 500.

And this is the error I get in the error log

PHP Fatal error: Uncaught Error: Call to undefined function get_option() in /nas/content/live/adsplugintest/wp-content/plugins/custom-ads-plugin/styles.php:11\nStack trace:\n#0 {main}\n thrown in /nas/content/live/adsplugintest/wp-content/plugins/custom-ads-plugin/styles.php on line 11, referer: domain.com/wp-includes/options.php/

1
Call to undefined function get_option that's the problem. what's unclear about it? - user9487972
The function actually exists. Do I have to register it in the stylesheet somehow, or what do I do? - AliMahachkala
it does not exist as far as the scipt calling it is concerned. is the file that it is in included here? - user9487972
you cannot use built-in funtion of wp like you want, you need to include WP code developer.wordpress.org/reference/functions/get_option/#source - Temani Afif
it's in the plugin file, that has this stylesheet enqueued in the very beginning function ads_styles() { wp_register_style('ads_stylesheet', plugins_url('styles.php', FILE)); wp_enqueue_style('ads_stylesheet'); } the stylesheet works fine until the I add the option. Other options everywhere else work fine. - AliMahachkala

1 Answers

1
votes

You'll need to include the rest of WordOress in order for the function get_option to be available to your dynamic stylesheet.

Even though it's called by a WordPress template, it's treated as it's own isolated request by the server, and has to be evaluated as such. (Meaning the functions used in it all have to be included somewhere along the line.)

<?php

// wp-content/plugins/custom-ads-plugin/styles.php
require ('../../../wp-blog-header.php');

header("Content-type: text/css; charset: UTF-8");

$myoptions = get_option( 'custom_ads' );