Solved

PHP OOP Connect function to another

Posted on 2014-02-22
4
457 Views
Last Modified: 2014-02-23
I'm not sure how to ask this question and I don't have a real world example or well written code to show you, but I will do my best.
I have a Wordpress plugin that has a Functions.php file.
Function myContact() within the functions.php file creates a table.  
Each of the tables has a specific ID.
Each <tr> within the table has a specific ID.
Like so:
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>

</table>";
}

Open in new window


I don't want to mess with the plugin core functions, cause if there is an update, I will loose the work. As you know.
So, I want to learn how to attach a custom function to another function. I want to attach my function to the table in the function above.  Like so:
My Custom Function.PHP file
function myCustomFunction() {
echo '<tr id='field_three'>
<td>EMAIL</td><td>CELL PHONE</>
</tr>
<tr id='field_four'>
<td>BIRTHDAY</td><td>NICK NAME</>
</tr>
}
add_action( 'myCustomFunction', myContact' ); INSERT AFTER tr id='field_two'

Open in new window


I want to insert my function after a specific <tr id> in the original function.
Does that make sense???
0
Comment
Question by:rgranlund
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39879707
I don't think that is going to work. Usually the function you are overriding should be written to support overridding - something like
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>";
 if (function_exists('customMyContact')) {
   customMyContact();
 }
echo "
</table>";
}

Open in new window

                                 
You can now extend the function by defining your customMyContact function.

However because the pluggin generates the complete table in one function with no external calls your only option is to replace the function with your own, modify the function to call your own or use javascript to modify the table at load time.

Either of the first two options fall foul of the pluggin upgrade overwrite problem.

The javascript option will work - basically you add some javascript code that appends the required information to the table. With JQuery it would be something like this

<script type="text/javascript">
// POPULATE YOUR HTML STRING HERE WITH PHP CODE INSERTS
var html="tr id='field_three'><td>EMAIL</td><td><?php echo $cell_phone;?></></tr><tr id='field_four'><td><?php echo $dob;?></td><td><?php echo $nickname;?></></tr>";
$(function() {
  $('#table_one').append(html);
});
</script>

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 39879721
OK, another question, what does the "10' and '2' signify at the end?
add_action('personal_details_list', 'custom_purchase_details', 10, 2);
0
 
LVL 55

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 39880631
The 10 paramter refers to the priority given to the action - the lower the number the sooner it executes. The default is 10

The last parameter refers to the number of parameters the hook accepts.

You can read more about the add_action call here
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question