admin管理员组

文章数量:1279057

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I'm trying to create a few custom post types, and instead of hard-coding their names I just want to define them as a globally scoped variable. I thought I understood the scope but apparently not. The code below results in an error of Undefined variable:

$filmLabel = "Films";
$showLabel = "Shows";
function CreatePostTypes() {
    register_post_type( $filmLabel, GenerateFilmType($filmLabel));
    register_post_type( $showLabel, GenerateFilmType($showLabel));
}
add_action( 'init', 'CreatePostTypes' );

I've tried using $GLOBAL as well, with the same result. Can anyone spot what's wrong?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I'm trying to create a few custom post types, and instead of hard-coding their names I just want to define them as a globally scoped variable. I thought I understood the scope but apparently not. The code below results in an error of Undefined variable:

$filmLabel = "Films";
$showLabel = "Shows";
function CreatePostTypes() {
    register_post_type( $filmLabel, GenerateFilmType($filmLabel));
    register_post_type( $showLabel, GenerateFilmType($showLabel));
}
add_action( 'init', 'CreatePostTypes' );

I've tried using $GLOBAL as well, with the same result. Can anyone spot what's wrong?

Share Improve this question asked Oct 12, 2021 at 15:42 FluxianFluxian 1809 bronze badges 10
  • 2 Why do you want to do this? Using global vars is generally bad practice. The accepted answer on this post gives a good explanation why. – vancoder Commented Oct 12, 2021 at 17:17
  • @vancoder Generally I'm sure it is bad practice. Randomly hard-coding magic values everywhere is worse practice, I'm sure. That's why. – Fluxian Commented Oct 12, 2021 at 19:16
  • 1 See stackoverflow/questions/15687363/… – vancoder Commented Oct 12, 2021 at 20:36
  • 1 So the actual real reason why using globals is a bad idea is because of syntactically useless it is to do so. Cheers – Fluxian Commented Oct 12, 2021 at 21:55
  • @Fluxian "Randomly hard-coding magic values" - a post type name/slug is not random. And it shouldn't change, otherwise all your data gets lost. Instead of the define() which seems a bit dated in modern PHP, I would simply go with a public class constant. – kero Commented Oct 21, 2021 at 8:14
 |  Show 5 more comments

1 Answer 1

Reset to default 1

Globals are generally discourgaged. You can use define instead.

<?php
define("FILM_LABEL", "Films");
define("SHOW_LABEL", "Shows");
function CreatePostTypes() {
    register_post_type( FILM_LABEL, GenerateFilmType( FILM_LABEL ) );
    register_post_type( SHOW_LABEL, GenerateFilmType( SHOW_LABEL ) );
}
add_action( 'init', 'CreatePostTypes' );

本文标签: phpVariable global scope