我尝试将所有代码更改为OOP,我是OOP和PHP的初学者。这很有效,但当我将代码更改为OOP时,我可以在更多地方使用我的类
function advanza_form_shortcode() {
// Display the Advanza form with a shortcode
$site_id = get_option(\'advanza_form_site_id\');
$affiliate_id = get_option(\'advanza_form_affiliate_id\');
$redirection = get_option(\'advanza_form_thankyou_page\');
$url = "https://advanzaservices.com/wp/singleform.php?site_id={$site_id}&affiliate_id={$affiliate_id}&thankyou={$redirection}";
$response = wp_remote_get( $url );
if ( is_array( $response ) ) {
$header = wp_remote_retrieve_headers($response[\'headers\']); // array of http header lines
$body = wp_remote_retrieve_body($response); // use the content
$status_code = wp_remote_retrieve_response_code($response);
if($status_code == 200) {
return $body;
} else {
return $status_code;
}
}
} add_shortcode( \'advanza-form\', \'advanza_form_shortcode\' );
我不明白为什么这不起作用,我试了很多次,但似乎我没有明白。class advanza_form {
public $site_id;
public $affiliate_id;
public $redirection;
public $url;
public function __construct() {
$this->site_id = get_option(\'advanza_form_site_id\');
$this->affiliate_id = get_option(\'advanza_form_affiliate_id\');
$this->redirection = get_option(\'advanza_form_thankyou_page\');
$this->url = "https://advanzaservices.com/wp/singleform.php?site_id={$this->site_id}&affiliate_id={$this->affiliate_id}&thankyou={$this->redirection}";
}
public function display() {
$response = wp_remote_get( $this->url );
$header = wp_remote_retrieve_headers($this->response[\'headers\']); // array of http header lines
$status_code = wp_remote_retrieve_response_code($this->response);
try {
if($status_code == 200) {
$body = wp_remote_retrieve_body($response);
} else {
return $status_code;
}
} catch (Exception $e) {
echo \'Caught exception: \', $e->getMessage(), "\\n";
}// end try/catch
return $body;
}
function debug() {
echo \'Site ID: \' . $this->site_id . \'<br>\';
echo \'Affiliate ID: \' . $this->affiliate_id . \'<br>\';
echo \'Redirection: \' . $this->redirection . \'<br>\';
echo \'URL: \' . $this->url . \'<br>\';
}
}
在这里,我创建了我的短代码function advanza_form_shortcode() {
// Display the Advanza form with a shortcode
$form = new advanza_form;
$form->display();
// $form->debug();
}
add_shortcode( \'advanza-form\', \'advanza_form_shortcode\' );