How to properly include php files inside wordpress plugin - php

Hi I am creating my first custom wordpress plugin.
I have run into an issue when I try to create a link in an included file to one of the plugin menus in the dashboard.
Here is my code for the link
<p>Add a new record</p>
>
As you may have noticed it is using a full URI.
I have tried using ?page=add-members as the href source and it seems to work.
I am wondering if that is the appropirate method to do this or if there is a more standardized way.
Thanks in advance.

I believe what you are looking for is the get_site_url function.
<?php echo get_site_url(); ?>
So ideally you would link it like:
<p>Add a new record</p>
You can read about it on the docs.

Related

Wordpress Custom Fields not displaying the section

I have added a new layout in the Wordpress Custom fields plugin, and after adding it I have added the fields in the corresponding php file too. But for some reason, its printing the code in the browser. I am not sure what I am doing wrong here. I am new to WordPress, so can someone please help me?
Screenshots attached for reference.
PHP file code - https://codeshare.io/GklExO
Figured it out, I had to add an image tag and then it worked fine.
However, there are other PHP files in the site, where
<?php the_sub_field('image'); ?> displays the image, but in this particular case I had to write it as
<img src="<?php echo esc_url($image['url']); ?>"/>
Still trying to understand how this works.

How to add code after "</article>" in content-page.php in Wordpress?

I am using the Shareaholic plugin and they give you a block of code to add the sharing buttons anywhere on your site.
[shareaholic app="share_buttons" id="7726121"]
[shareaholic app="recommendations" id="7962129"]
I want to add this code after </article> tag in the file "content-page.php" in Wordpress. When I do it then nothing happens. When I check the page source then I can't find the code anywhere. It happens even when I add some random word after </article> tag.
How can I make this work?
Thanks!
These are short code and you can't call them directly you need to do it like this.
echo do_shortcode('[shareaholic app="share_buttons" id="7726121"]');
echo do_shortcode('[shareaholic app="recommendations" id="7962129"]');

Is it possible to call PHP in html?

I have a PHP that returns some HTML code, for example, if I execute this in the browser,
http://www.myserver.com/myscript.php?size=300
this will return some html code like this,
'<div><img src="..." /></div>'
I am wondering if it's possible I can call this php script in my html code directly? For example, if I want to use the output from the PHP script in my Wordpress sidebar widget?
You can use file_get_contents() like this:
<div class="sidebar">
<?php
$html = file_get_contents("http://www.myserver.com/myscript.php?size=300");
echo $html;
?>
</div>
If your Current page's extension is not php, you cannot call php in it directly.
You have to use some javascript/jquery/ajax to run the php file externally and load the data into a class or id using one of those javascript based code.
If your current page is already a php file than,you can use include(); or require(); functions and it does the job.
above answers are correct, but that might be not concerned for wordpress, that are of core php functionality.
i have a different way for sidebar use, that's provide by wordpress already.
it is an inbuilt functionality, please take a look towards my answer.
Show active sidebar in Wordpress
How to make wordpress sidebar
you can found ::
try this one, it is different way of using sidebar
[1] put your all "left_bar"(sidebar) code in a new file named "sidebar-left_bar"
[2] save it with header.php, function.php and all files (saving location)
[3] now just use <?php get_sidebar( 'left_bar' ); ?> where you want to use
Thanks

Relative pathing

Creating a wordpress sight for work to display some info. For convience i have included the following php code
<?php include 'wp-includes/fancyboxstart.php'; ?>
that file loads all the JS, and defines the div id for my fancybox img display. Recently i have changed the permalinks on my WordPress to get rid of the awful page=1 html links. Now i know to fix all my images simply by adding a "/" to the front on my img srcs. My problem is now that i have changed the permalinks it is no longer loading the JS page from above and my Image displayer no longer works =[
I have tried directly linking to it with
`<?php include 'http://premiumshotguns.com/wp-includes/fancyboxstart.php'; ?>`
but no luck. I know it's probably a simple fix but i am not seeing it! any help would be appreciated!
You probably need to include your theme path.
<?php include bloginfo('template_directory').'/wp-includes/fancyboxstart.php'; ?>

mixpanel analytic in wordpress blog not working

I am trying to use mixpanel for analytic in my Wordpress blog (just for learning).
My blog is all php.
I got the code from mixpanel's website and put it in my header.php file.
<!-- start Mixpanel --><script type="text/javascript">(function(c,a){window.mixpanel=a;var b,d,h,e;b=c.createElement("script");b.type="text/javascript";b.async=!0;b.src=("https:"===c.location.protocol?"https:":"http:")+'//cdn.mxpnl.com/libs/mixpanel-2.0.min.js';d=c.getElementsByTagName("script")[0];d.parentNode.insertBefore(b,d);a._i=[];a.init=function(b,c,f){function d(a,b){var c=b.split(".");2==c.length&&(a=a[c[0]],b=c[1]);a[b]=function(){a.push([b].concat(Array.prototype.slice.call(arguments,0)))}}var g=a;"undefined"!==
typeof f?g=a[f]=[]:f="mixpanel";g.people=g.people||[];h="disable track track_pageview track_links track_forms register register_once unregister identify name_tag set_config people.set people.increment".split(" ");for(e=0;e<h.length;e++)d(g,h[e]);a._i.push([b,c,f])};a.__SV=1.1})(document,window.mixpanel||[]);
mixpanel.init("MYKEY");</script><!-- end Mixpanel -->
Here, MYKEY is my actual key.
Now, I want to see page views for each of my blog posts.
I am putting following code in page.php, which gets called each time a post is viewed (afaik).
<script type="php">
mixpanel.track("Viewed Post")
</script>
But I am not getting any data back. Can someone please help me here?
I am using Mixpanel on my blog (along Google Analytics). Some suggestions
Make sure you've put the mixpanel code before the head closing statement </head>. In many themes you will find a lot of code below the </head> for example the <body class="<?php hybrid_body_class(); ?>"> in the case of hybrid based themes
I am not sure why you are using <script type="php"> the usual is <script type="text/javascript">
Hope this helps!
in all my wordpress website i want to delete this mixpanel, someone can tell me how i can do that?
MixPanel does not work well with Wordpress blog. I would use Google Analytics.

Categories